Simple and Naive 8ms Java O(n) without Regex or indexOf, beat 86.58%


  • 0
    M
    public boolean checkRecord(String s) {
        int aCount = 0, lCount = 0;
        for (char c : s.toCharArray()) {
            if (c == 'A') {
                if (aCount == 1) return false;
                aCount++;
                lCount = 0;
            }
            if (c == 'L') {
                if (lCount == 2) return false;
                lCount++;
            }
            if (c == 'P')  lCount = 0;
        }
        return true;
    }
    

Log in to reply
 

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