Java simple solution O(n)


  • 0
    M
    public class Solution {
        
        public boolean checkRecord(String s) {
            int countA = 0;
            int countL = 0;
    
            for (int i = 0; i < s.length(); i++) {
                
                char c = s.charAt(i);
                
                if (c == 'A') countA++;
                
                if (c == 'L') {
                    countL++;
                    if (countL > 2) break;
                } else {
                    countL = 0;
                }
                
            }
            
            if (countA > 1 || countL > 2) return false;
            
            return true;
        }
        
    }
    

Log in to reply
 

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