Python, Straightforward with Explanation


  • 0

    To count all occurrences of 'A' in a list, we can use S.count('A').

    To determine whether there are at most 2 consecutive L's, we can search for 'LLL'. If a string has 3 or more consecutive L's, it will definitely contain 'LLL'.

    Alternatively, we could look at S[i:i+3] for 'LLL', or we could look at groups len(list(v)) >= 3 for k, v in itertools.groupby(S) if k == 'L'.

    def checkRecord(self, S):
        return S.count('A') <= 1 and 'LLL' not in S
    

Log in to reply
 

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