Short C# solution with only one extra local boolean parameter defined -- O(N)


  • 0
    R
        public bool CheckRecord(string s) {
            bool foundA = false;
            for(int i = 0; i <s.Length; i++)
            {
                if((i < s.Length-2 && s[i] == 'L' && s[i+1] == 'L' && s[i+2] == 'L') || foundA && s[i] == 'A') return false;
                foundA = foundA || s[i] == 'A';
            }
            
            return true;
        }
    

    We can use a one line solution below two. The problem would be that we iterate the string more than one. However, solution above we iterate exactly once. Although both solutions will be of O(N). The one above will be more optimal.

        public bool CheckRecord(string s) {
            return !(s.IndexOf("A") != s.LastIndexOf("A") || s.Contains("LLL"));
        }
    

Log in to reply
 

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