Student Attendance Record I
💻 coding

Student Attendance Record I

1 min read 157 words
1 min read
ShareWhatsAppPost on X
  • 1A student can be rewarded if their attendance record contains at most one 'A' (absent).
  • 2The record must not have more than two consecutive 'L' (late) entries.
  • 3The solution checks the attendance record in linear time with constant space complexity.

AI-generated summary · May not capture all nuances

Key Insight
AskGif

"A student can be rewarded if their attendance record contains at most one 'A' (absent)."

Student Attendance Record I

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

'A' : Absent.

'L': Late.

'P' : Present.

A student could be rewarded if his attendance record doesn't contain more than one 'A' (absent) or more than two continuous 'L' (late).

You need to return whether the student could be rewarded according to his attendance record.

Example 1:

Input: "PPALLP"

Output: True

Example 2:

Input: "PPALLL"

Output: False

Solution:

using System;
using System.Collections.Generic;
using System.Text;

namespace LeetCode.AskGif.Easy.String
{
 public class CheckRecordSoln
 {
 public bool CheckRecord(string s)
 {
 int absent = 0; 
 for (int i = 0; i < s.Length; i++)
 {
 if(s[i] == 'A')
 {
 absent++;
 }
 else
 {
 if (i < s.Length - 2)
 {
 if(s[i] == 'L' && s[i+1] == 'L' && s[i+2] == 'L')
 {
 return false;
 }
 }
 }
 }

 return absent <= 1;
 }
 }
}

Time Complexity: O(n)

Space Complexity: O(1)

Enjoyed this article?

Share it with someone who'd find it useful.

ShareWhatsAppPost on X

AskGif

Published on 11 May 2020 · 1 min read · 157 words

Part of AskGif Blog · coding

You might also like