Java using String methods


  • 0
    Y
    public class Solution {
        // notes: 
        // using String methods: equals, toLowerCase and toUpperCase 
        public boolean detectCapitalUse(String word) {
            String subStr = word.substring(1);
            char first = word.charAt(0);
            if (first >= 'A' && first <= 'Z') { 
                return allLowerCase(subStr) || allUpperCase(subStr);
            } else {
                return allLowerCase(subStr);     
            } 
        } 
        
        boolean allLowerCase(String s) {
            return s.equals(s.toLowerCase());     
        }
        
        boolean allUpperCase(String s) {
            return s.equals(s.toUpperCase());     
        }
    }
    

Log in to reply
 

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