Simple Java solution


  • 0
    J
    public class Solution {
    	public boolean detectCapitalUse(String word) {
    		int length = word.length();
    	
    		boolean isAllUpper = true;
    		for(int i = 0; i < length && isAllUpper == true; i++)
        			if(Character.isLowerCase(word.charAt(i)))
        				isAllUpper = false;
        
    	        boolean isAllLower = true;
        		for(int i = 0; i < length && isAllLower == true; i++)
        			if(Character.isUpperCase(word.charAt(i)))
        				isAllLower = false;
        
    	        boolean isCamelNotation = Character.isUpperCase(word.charAt(0));
        		for(int i = 1; i < length && isCamelNotation == true; i++)
        			if(Character.isUpperCase(word.charAt(i)))
        				isCamelNotation = false;
        
    	        return isAllUpper || isAllLower || isCamelNotation;
    	}
    
    	public static void main(String[] args) {
    		Solution s = new Solution();
    		System.out.println(s.detectCapitalUse("Leetcode"));
    	}
    }

Log in to reply
 

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