Student Attendance Record I

Easy
631.

Student Attendance Record I

Student Attendance Record I is an easy coding problem on strings, asked in TCS, Infosys and Cognizant interviews.

An attendance record is a string where each character is 'A' (absent), 'L' (late) or 'P' (present).

A student is eligible for an award when they were absent fewer than 2 days in total and were never late on 3 or more consecutive days.

Return true if the record earns an award.

Example 1

Input
s = "PPALLP"
Output
true
Explanation
One absence and never three L's in a row.

Example 2

Input
s = "PPALLL"
Output
false
Explanation
Three consecutive late days.

Example 3

Input
s = "AA"
Output
false
Explanation
Two absences.

Constraints

  • 1 <= s.length <= 1000
  • s[i] is 'A', 'L' or 'P'.
Write your solution here. Running it against the tests needs a free account — what you type comes with you.Sign in to run
/**
 * @param {string} s
 * @return {boolean}
 */
var checkRecord = function(s) {
    // Write your code here
    
};

Sign in to run your own test cases

Sign in

Student Attendance Record I — Solution in Python, Java & C++

Student Attendance Record I solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript. An easy problem on strings.

Student Attendance Record I is an easy coding problem on strings, asked in TCS, Infosys and Cognizant interviews.

  • Difficulty: Easy
  • Topics: Strings
  • Asked at: TCS, Infosys, Cognizant
  • Time limit: 2 s
  • Memory limit: 256 MB
  • Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby

Problem statement

An attendance record is a string where each character is 'A' (absent), 'L' (late) or 'P' (present).

A student is eligible for an award when they were absent fewer than 2 days in total and were never late on 3 or more consecutive days.

Return true if the record earns an award.

Example 1

Input: s = "PPALLP"
Output: true
Explanation: One absence and never three L's in a row.

Example 2

Input: s = "PPALLL"
Output: false
Explanation: Three consecutive late days.

Example 3

Input: s = "AA"
Output: false
Explanation: Two absences.

Constraints

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

Hints for Student Attendance Record I

  1. Hint 1

    Two independent checks: a total count and a run length.

  2. Hint 2

    Count 'A' as you go and bail out as soon as it reaches 2.

  3. Hint 3

    For the late rule, either keep a running streak or look back two characters.

How to solve Student Attendance Record I

Both rules are decidable in one pass: absences are a running total, and 'three in a row' only ever needs the current character and the two before it.

Approach

  1. Sweep the string keeping absent, the number of 'A' seen.
  2. At each 'L', check whether the two preceding characters are also 'L'; if so, return false immediately.
  3. After the sweep, return absent < 2.

Why it works

A run of three or more late days necessarily contains a position whose two predecessors are both 'L', so the look-back catches every violation. The absence rule is a plain total and needs no context at all.

Complexity

  • Time — O(n)
  • Space — O(1)

Pitfalls

  • absent <= 2 is wrong — two absences already disqualify.
  • Resetting a late streak on 'A' but not on 'P' (or the other way round) — any non-'L' breaks the run.

Reference solution

Python

def checkRecord(s: str) -> bool:
    absent = 0
    for i, c in enumerate(s):
        if c == "A":
            absent += 1
        if c == "L" and i >= 2 and s[i - 1] == "L" and s[i - 2] == "L":
            return False
    return absent < 2

Java

public static boolean checkRecord(String s) {
    int absent = 0;
    for (int i = 0; i < s.length(); i++) {
        char c = s.charAt(i);
        if (c == 'A') absent++;
        if (c == 'L' && i >= 2 && s.charAt(i - 1) == 'L' && s.charAt(i - 2) == 'L') return false;
    }
    return absent < 2;
}

C++

bool checkRecord(string s) {
    int absent = 0;
    for (int i = 0; i < (int) s.size(); i++) {
        if (s[i] == 'A') absent++;
        if (s[i] == 'L' && i >= 2 && s[i - 1] == 'L' && s[i - 2] == 'L') return false;
    }
    return absent < 2;
}

JavaScript

var checkRecord = function(s) {
    var absent = 0;
    for (var i = 0; i < s.length; i++) {
        var c = s.charAt(i);
        if (c === "A") absent++;
        if (c === "L" && i >= 2 && s.charAt(i - 1) === "L" && s.charAt(i - 2) === "L") return false;
    }
    return absent < 2;
};

Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.

All 515 strings problems · the whole catalogue

Learn the technique: Strings