Java using two state flags


  • 0
    Z
    public class Solution {
        public boolean detectCapitalUse(String word) {
            if (word==null || word.length()==0) return true;
            boolean first = false;
            boolean all = false;
            for (int i = 0; i < word.length(); i++) {
                char c = word.charAt(i);
                if (c>='A' && c<='Z') {
                    if (i == 0) {
                        first = true;
                        all = true;
                    } else {
                        if (!all) return false;
                    }
                } else {
                    if (all && i>1) return false;
                    all = false;
                }
            }
            return true;
        }
    }
    

Log in to reply
 

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