Student Attendance Record I

Updated on 04 July, 2025
Student Attendance Record I header image

Problem Statement

In this task, you are tasked with determining whether a student qualifies for an attendance award based on a string s that records their daily attendance. This string consists of characters:

  • 'A' for Absent
  • 'L' for Late
  • 'P' for Present

To qualify for the award, a student must satisfy the following criteria:

  • They should be absent ('A') on strictly fewer than two days in total.
  • They should not have three or more consecutive days marked as late ('L').

The function should return true if the student is eligible for an award, based on these conditions, and false otherwise.

Examples

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'.

Approach and Intuition

  1. Count the total number of 'A's in the string s. If there are 2 or more, we can return false since the student disqualifies based on the number of absences.

  2. Check for sequences of three or more 'L's. We can iterate through the string and keep a counter for consecutive 'L's. If at any point the counter reaches 3, we return false since the student does not meet the criteria regarding consecutive lates.

  3. If after scanning through the string, neither of the disqualifying conditions are met, we return true, indicating the student is eligible for the award.

By iterating through the string only once and applying the conditions as we go, this approach efficiently determines the eligibility of the student for the award. This direct approach adheres to the problem's constraints and requirements without extra computational steps.

Solutions

  • Java
java
public class Solution {
    public boolean isEligible(String record) {
        return !record.matches(".*(A.*A|LLL).*");
    }
}

The provided Java solution determines the eligibility of a student's attendance record. This method isEligible checks if the record string does not contain more than one 'A' or a sequence of 'LLL'.

  • Analyze the isEligible method:
    • This method relies on Java regular expression capabilities.
    • The matches function checks the entire string against the pattern provided.
    • The regular expression ".(A.A|LLL).*" checks for the presence of two 'A's separated by any characters or a sequence of three consecutive 'L's within the student attendance record.
    • If such a pattern is found, matches returns true, and isEligible will return false because it negates the result (! operator). If the pattern is not found, it returns true, indicating the student meets the attendance criteria.

By executing this method and passing the student attendance record as a parameter, quickly determine if a student meets the required attendance criteria based on the specified conditions.

Comments

No comments yet.