Given A word, you need to judge whether the usage of capitals in it are right or not.
We define the usage of capitals in a word to being right when one of the following cases holds:
- All letters in this word is capitals, like "USA".
- All letters in this word is not capitals, like "Leetcode".
- Only the first letter of this word was capital if it had more than one letter, like "Google".
Otherwise, we define that this word doesn ' t use capitals in a right-to-do.
Example 1:
Input: "USA" output:true
Example 2:
Input: "FlaG" Output:false
Note:the input is a non-empty word consisting of uppercase and lowercase Latin letters.
Test instructions: give you a string to determine whether it conforms to the specification
This is my code. It feels a little stupid.
classSolution { Public: BOOLDetectcapitaluse (stringword) { if(word[0] >= $&& word[0] <= -){ intn =0; intm =0; for(inti =1; I < word.size (); i++){ if(Word[i] <= -) M=1; ElseN=1; } if(n + M >1) return false; } Else{ for(inti =1; I < word.size (); i++){ if(Word[i] <'a') return false; } } return true; }};
There is also a C + + regular expression made out.
class Solution {public: bool detectcapitaluse (string word) { return regex_match (Word, regex ("[a-z]+|[ a-z]+| [A-z] [a-z]*]);} ;
Discuss user Python answers:
When adding a Boolean in Python it's changed to a 1 or 0. You can loop through each character and sum the amount of upper case letters seen.
def detectcapitaluse (self, word): = 0 for in Word: + = c.isupper ( )returnor or and word[0].isupper ()) # return all_lowercase or All_ Uppercase or Only_the_first_is_upper
520. Detect Capital