Simple Java Approach (Beats 90% Java Submission)


  • 0
    T
    class Solution {
        public boolean detectCapitalUse(String word) {
            word = word.trim();
            for(int i = 0; i < word.length(); i++) {
                if(word.charAt(i) >= 'A' && word.charAt(i) <= 'Z') {
                    if(i > 0 && (word.charAt(i-1) < 'A' || word.charAt(i-1) > 'Z')) {
                        return false;
                    }
                } else if(word.charAt(i) >= 'a' && word.charAt(i) <= 'z') {
                    if(i > 1 && (word.charAt(i-1) < 'a' || word.charAt(i-1) > 'z')) {
                        return false;
                    }
                } 
            }
            return true;
        }
    }
    

Log in to reply
 

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