Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How leetcode detects uppercase letters

Shulou Source: shulou.com Published: 2022-06-01 01:10:31 09月30日 Update

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.

Tags: Uppercase letters words uppercase letters characters articles input detection only strings lowercase more examples topics output good practical code content ideas Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Xiaomi MySQL Shulou Technology Docker