{x}
blog image

Student Attendance Record I

You are given a string s representing an attendance record for a student where each character signifies whether the student was absent, late, or present on that day. The record only contains the following three characters:

  • 'A': Absent.
  • 'L': Late.
  • 'P': Present.

The student is eligible for an attendance award if they meet both of the following criteria:

  • The student was absent ('A') for strictly fewer than 2 days total.
  • The student was never late ('L') for 3 or more consecutive days.

Return true if the student is eligible for an attendance award, or false otherwise.

 

Example 1:

Input: s = "PPALLP"
Output: true
Explanation: The student has fewer than 2 absences and was never late 3 or more consecutive days.

Example 2:

Input: s = "PPALLL"
Output: false
Explanation: The student was late 3 consecutive days in the last 3 days, so is not eligible for the award.

 

Constraints:

  • 1 <= s.length <= 1000
  • s[i] is either 'A', 'L', or 'P'.

Solution Explanation

This problem asks to determine if a student's attendance record is eligible for an award based on two conditions:

  1. The number of absences ('A') is strictly less than 2.
  2. There are no instances of 3 or more consecutive late days ('L').

The solutions provided leverage the efficiency of built-in string functions to achieve a linear time complexity. Let's break down the code:

Approach

All solutions follow the same approach:

  1. Count Absences: Determine the total number of 'A's in the attendance record. This is done using s.count('A') in Python, s.indexOf("A") == s.lastIndexOf("A") (which checks if there's only one or zero 'A's) in Java and TypeScript, count(s.begin(), s.end(), 'A') in C++, and strings.Count(s, "A") in Go.

  2. Check for Consecutive Late Days: Check if the substring "LLL" exists within the attendance record. This uses 'LLL' not in s in Python, !s.contains("LLL") in Java and TypeScript, s.find("LLL") == string::npos in C++, and !strings.Contains(s, "LLL") in Go.

  3. Combine Conditions: The final result is true only if both conditions are met (less than 2 absences AND no "LLL" sequence).

Code Explanation (Python Example)

class Solution:
    def checkRecord(self, s: str) -> bool:
        return s.count('A') < 2 and 'LLL' not in s

This Python code directly implements the approach. s.count('A') efficiently counts the occurrences of 'A', and 'LLL' not in s efficiently checks for the presence of "LLL". The and operator ensures both conditions must be true for the function to return true.

Time and Space Complexity Analysis

  • Time Complexity: O(n), where n is the length of the input string s. The count() and in (or equivalent functions in other languages) operations have a linear time complexity in the worst case (they need to scan the entire string).

  • Space Complexity: O(1). The solution uses a constant amount of extra space regardless of the input string's length. The space used by variables is not dependent on the size of the input.

Other Language Code Explanations

The Java, C++, Go, and TypeScript solutions are analogous to the Python solution. They all use built-in string functions to achieve the same linear time complexity and constant space complexity. The slight variations in syntax reflect the differences in the languages. For example, Java uses indexOf and contains, while C++ uses count and find. The core logic remains the same across all languages.