Java Easy to Understand without indexOf and contains methods


  • 1
    V
    public class Solution {
        public boolean checkRecord(String s) {
            if(s.length() < 2) return true;
            int ab = 0;
            if(s.charAt(0) == 'A') ab++;
            if(s.charAt(1) == 'A') ab++;
            if(ab > 1) return false;
            for(int i = 2; i < s.length(); i++){
                if(s.charAt(i) == 'A') ab++;
                if(ab > 1) return false;
                if(s.charAt(i-2) == 'L' && s.charAt(i-1) == 'L' && s.charAt(i) == 'L') return false;
            }
            return true;
        }
    }
    

Log in to reply
 

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