algoadvance

You are given a string representing an attendance record for a student. The record only contains the following three characters:

A student could be rewarded if their attendance record does not contain:

  1. More than one ‘A’ (absent).
  2. More than two continuous ‘L’ (late).

You need to implement a function checkRecord(s: str) -> bool that checks whether the student’s record is eligible for an attendance award.

Clarifying Questions

No additional clarifications are needed, as the problem statement is clear. We need to return True if the student is eligible based on the given conditions, and False otherwise.

Strategy

  1. Check for ‘A’s: We need to ensure the string contains no more than one ‘A’.
  2. Check for continuous ‘L’s: We need to ensure that there are no instances where ‘L’ appears more than twice in a row.

To achieve this, we can:

  1. Count the occurrences of ‘A’ in the string.
  2. Use a sliding window technique or string operations to check for three consecutive ‘L’s.

Code

Here’s the Python implementation:

def checkRecord(s: str) -> bool:
    # Check for more than one 'A'
    if s.count('A') > 1:
        return False
    
    # Check for three consecutive 'L's
    if 'LLL' in s:
        return False
    
    return True

Time Complexity

Hence, the overall time complexity of the algorithm is O(n).

By keeping the solution straightforward using built-in string operations, it remains efficient and easy to understand.

Try our interview co-pilot at AlgoAdvance.com