Straight-forward Java Solution


  • 0
    W

    Basic idea:

    1. Map upper-case to "1", and lower-case to "0";
    2. Compare correct patterns("0", "100000...", "0000....", "11111....")

    Straight forward but computationally costly.

    public class Solution {
        public boolean detectCapitalUse(String word) {
            final int b = (int)'Z';
            int len = word.length();
            String s1, r1, r2, r3;
            s1 = r1 = r2 = r3 = "";
            for(int i= 0; i < len; i++) {
                s1 += ((int)word.charAt(i)) <= b ? "1" : "0";
                r1 += "0";
                r3 += "1";
                if (i == 0) {
                    r2 += "1";
                    continue;
                }
                r2 += "0";
            }
            return (s1.equals("0") || s1.equals(r1) || s1.equals(r2) || s1.equals(r3));
        }
    }
    

Log in to reply
 

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