Java 1 line with explanation


  • 0
    G

    my code:

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

    explanation: there are two case:
    1.The strings are all capitalized,so you just compare the original string with the uppercase string, just like "USA"=="USA".toUpperCase(), but "UsA"!="UsA".toUpperCase().
    2.Except for the first letter,if the remaining strings are lowercase, Whether the first letter is uppercase or lowercase, the result is true, for example, "Google" and "google" is all true.


Log in to reply
 

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