3 Lines


  • 44
    public class Solution {
        public boolean detectCapitalUse(String word) {
            int cnt = 0;
            for(char c: word.toCharArray()) if('Z' - c >= 0) cnt++;
            return ((cnt==0 || cnt==word.length()) || (cnt==1 && 'Z' - word.charAt(0)>=0));
        }
    }
    

  • 13

    C++ Version

    class Solution {
    public:
        bool detectCapitalUse(string word) {
            int cnt = 0;
            for (char c : word) if (isupper(c)) ++cnt;
            return !cnt || cnt == word.size() || cnt == 1 && isupper(word[0]);
        }
    };
    

  • 0
    W

    My C++ solution that may return early.

        // AAA
            auto isUpper =[](const string &word) {
              for (auto x : word) {
                  if (x >= 'a')
                      return false;
              }
              return true;
            };
            // Aaa or aaa
            auto isLower = [](const string &word) {
              for (int i = 1; i < word.size(); ++i) {
                  if (word[i] < 'a')
                      return false;
              }
              return true;
            };
            return isUpper(word) || isLower(word);
    

  • 0

    In case of e.g. "AbCde" this won't be lazy, i.e. after C it should return false without visiting the rest of the characters.
    You could do that either via streams or regexes:

    boolean detectCapitalUse(String word) {
      return word.chars().allMatch(Character::isUpperCase)           
          || word.chars().skip(1).allMatch(Character::isLowerCase);
    }
    

    boolean detectCapitalUse(String word) {
      return word.matches("\\p{Lu}+|\\p{Lu}?\\p{Ll}+");
    }
    

  • 1
    G

    @Chidong you thought is great,and show my 1 line code:

        public boolean detectCapitalUse(String word) {
           return word.substring(1).equals(word.substring(1).toLowerCase())||word.equals(word.toUpperCase());
           }
    

  • 0

    Based on the rule, we can check the 2 leading letter, there is three situations:
    UU, following letters must be all upper;

    UL, following letters must be all lower;
    LL, same be lower;

    LU, false;

        public boolean detectCapitalUse(String word) {
        
          if(word.length() <= 1) return true;
          char[] cs = word.toCharArray();
          
          boolean followUpper = true;
          if(Character.isLowerCase(cs[1])){
            followUpper = false;  
          } else {
            if(Character.isLowerCase(cs[0])){
              return false;    
            } 
          }
          
          for(int i = 2; i < cs.length; i++){
            if(followUpper && Character.isLowerCase(cs[i])) return false;
            if(!followUpper && Character.isUpperCase(cs[i])) return false;
          }
          
          return true;
        }

  • 14

    The same as @Gene20 , easier to understand..
    Just check the first letter, then compare the substring from second letter

    If first letter is upper case, 2 possible valid string:

    1. All upper cases:
      word.substring(1).equals(word.substring(1).toUpperCase())

    2. All lower cases from 2nd letter:
      word.substring(1).equals(word.substring(1).toLowerCase())

    If first letter is lower case, 1 possible valid string:

    1. All lower cases:
      word.substring(1).equals(word.substring(1).toLowerCase())
        public boolean detectCapitalUse(String word) {
            if (word.length() == 1) return true;
            return Character.isUpperCase(word.charAt(0))? word.substring(1).equals(word.substring(1).toUpperCase()) ||
                      word.substring(1).equals(word.substring(1).toLowerCase()) : 
                      word.substring(1).equals(word.substring(1).toLowerCase());      
         }
    

  • 2
    S

    @wuyangbu I love this one


  • 0
    S

    just going by the rules in the question

    public boolean detectCapitalUse(String word) {
            
            if(word.equals(word.toUpperCase())){
                return true;
            }else if(word.equals(word.toLowerCase()))
                return true;
            else if(word.charAt(0)==word.toUpperCase().charAt(0)){
                return word.substring(1).equals(word.substring(1).toLowerCase());
            }
            return false;
        }
    

  • 0
    R

    Masterpiece!


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.