520. Detect Capital ' Easy '

Source: Internet
Author: User

520. Detect Capital ' Easy '

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:

    1. All letters in this word is capitals, like "USA".
    2. All letters in this word is not capitals, like "Leetcode".
    3. 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.

Solution One:

1 classSolution {2  Public:3     BOOLDetectcapitaluse (stringword) {4         if(Word.length () <=1) {5             return true;6         }7         8         BOOLFlag =false;9          for(inti =1; I < word.length (); ++i) {Ten             //starts with a lowercase letter, it must be followed by lowercase One             if(word[0] >='a'&& word[0] <='Z') { A                 if(Word[i] <'a'|| Word[i] >'Z') { -                     return false; -                 } the             } -             //start with a capital letter, the following categories discuss -             Else if(word[0] >='A'&& word[0] <='Z') { -                 if(i = =1&& Word[i] >='a'&& Word[i] <='Z') { +Flag =true; -                     Continue; +                 } A                 //description must be followed by lowercase at                 if(flag) { -                     if(Word[i] >='A'&& Word[i] <='Z') { -                         return false; -                     } -                 } -                 //the description must be capitalized later in                 Else { -                     if(Word[i] >='a'&& Word[i] <='Z') { to                         return false; +                     } -                 } the             } *         } $         Panax Notoginseng         return true; -     } the};

Solution Two:

1 classSolution {2  Public:3     BOOLDetectcapitaluse (stringword) {4         intSize=word.size (), count=0;5         if(size<=1)6             return true;7          for(inti =1; i < size; i++){8             if(word[i]>='a'&&word[i]<='Z')9count+=1;Ten             Else Onecount+=2; A         } -         if(count==size-1) -             return true; the         Else if(count==2* (size-1)) -             returnword[0]>='A'&&word[0]<='Z'; -         Else  -             return false; +     } -};

Referto the code @ Zhengchaojie.

From 1~size-1,if we meet with A-z,we add 1,else we add 2.Then we can get the result of the if the second to ll lowercase or all upcase.

Solution Three:

1 BOOLDetectcapitaluse (stringword) {2     Const Char*c =word.c_str ();3     if(Word.size () <=1)return true;4     if(*c <='Z'&& *c >='a') {5c = C +1;6          while(*c) {7             if(*c <='Z'&& *c >='A')return false;8c = C +1;9         }Ten}Else { Onec = C +1; A         if(*c <='Z'&& *c >='A') { -c = C +1; -              while(*c) { the                 if(*c <='Z'&& *c >='a')return false; -c = C +1; -             } -}Else { +c = C +1; -              while(*c) { +                 if(*c <='Z'&& *c >='A')return false; Ac = C +1; at             } -         } -     } -          -     return true; -}

Refer to the @ai977313677 code.

Solution Four:

1 classSolution {2  Public:3     BOOLDetectcapitaluse (stringword) {4         intCNT =0;5          for(CharC:word)if(Isupper (c)) + +CNT;6         return!cnt | | CNT = = Word.size () | | CNT = =1&& Isupper (word[0]);7     }8};

Refer to the @lzl124631x code.

520. Detect Capital ' Easy '

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.