Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How leetcode detects uppercase letters

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail how leetcode detects capital letters, Xiaobian thinks it is quite practical, so share it with you for reference, I hope you can gain something after reading this article.

Title Link

https://leetcode-cn.com/problems/detect-capital/

Title Description

Given a word, you need to decide whether the capitalization of the word is correct.

We define capitalized use of a word as correct if:

All letters are capital, like "USA."

All letters in words are not capitalized, such as "leetcode."

If the word contains more than one letter, only the first letter is capitalized, such as "Google."

Otherwise, we define the word incorrectly using capital letters.

Example 1:

Input: "USA" Output: True

Example 2:

Input: "FlaG" Output: False

Note: Input is a non-empty word consisting of upper and lower case Latin letters.

solution train of thought

Tag: string traversal

Traversing the string, recording the number greater than a and less than a, respectively

If all are greater than a, it means all lowercase, otherwise all uppercase

If only one is less than a, determine whether it is the first character.

Code class Solution { public boolean detectCapitalUse(String word) { int c1 = 0, c2 = 0; for(int i = 0; i

< word.length(); i++){ if(word.charAt(i) - 'a' >

= 0){ c1++; }else{ c2++; } } if(c1 == word.length() || c2 == word.length()){ return true; } return (1 == c2 && word.charAt(0) < 'a'); }} About "leetcode how to detect capital letters" This article is shared here, I hope the above content can be of some help to everyone, so that you can learn more knowledge, if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report