Java, define condition based on first letter case. / T : O(N), S : O(1)


  • 0
    J
    public class Solution {
        public boolean detectCapitalUse(String word) {
            if(word.length() == 1)
                return true;
            
            char[] chs = word.toCharArray();
            int index = Character.isLowerCase(chs[0]) ? 0 : 1;
            boolean wcase = Character.isLowerCase(chs[index]);
            for(int i = index+1; i < chs.length; i++){
                if(wcase != Character.isLowerCase(chs[i]))
                    return false;
            }
            
            return true;
        }
    }
    

Log in to reply
 

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