Easy understanding Java solution, 27ms, beats 96%


  • 0
     public boolean detectCapitalUse(String word) {
      if(word.length()<2) return true;
      boolean firstCapital = word.charAt(0)<='Z';
      boolean secondCapital = word.charAt(1)<='Z';
      if(!firstCapital&&secondCapital) return false;
      for(int i=2; i<word.length(); i++){
        if((word.charAt(i)<='Z')^secondCapital)
          return false;
      }
      return true;
      }
    

  • 0

    My solution is similar to yours:

    public boolean detectCapitalUse(String word) {
        if (word.equals(""))
            return false;
        if (word.length() == 1)
            return true;
        if (word.charAt(0) >= 'a' && word.charAt(1) < 'a')
            return false;
        for (int i = 2; i < word.length(); i++) {
            if ((word.charAt(1) - 'a' + 1) * (word.charAt(i) - 'a' + 1) < 0) {
                return false;
            }
        }
        return true;
    }

Log in to reply
 

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