Algorithm


Problem Name: 551. 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'.

 

Code Examples

#1 Code Example with Java Programming

Code - Java Programming


class Solution {
  public boolean checkRecord(String s) {
    int absentCount = 0;
    for (int i = 0; i  <  s.length(); i++) {
      char c = s.charAt(i);
      if (c == 'A') {
        absentCount++;
      } else if (c == 'L') {
        if (i > 1 && s.charAt(i - 1) == 'L' && s.charAt(i - 2) == 'L') {
          return false;
        }
      }
    }
    return absentCount  <  2;
  }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
s = "PPALLP"

Output

x
+
cmd
true

#2 Code Example with Javascript Programming

Code - Javascript Programming


/**
 * @param {string} s
 * @return {boolean}
 */
const checkRecord = function(s) {
  let anum = 0
  let plidx = -1
  for(let i = 0, len = s.length; i  <  len; i++) {
    if(s[i] === 'A') anum++
    if(anum > 1) return false
    if(s[i] === 'L') {
      if(i === 0) plidx = 0
      else if(s[i - 1] === 'L') {
        if(i - plidx + 1 > 2) return false
      } else {
        plidx = i
      }
    }
  }
  return true
};
Copy The Code & Try With Live Editor

Input

x
+
cmd
s = "PPALLP"

Output

x
+
cmd
true

#3 Code Example with Python Programming

Code - Python Programming


class Solution:
    def checkRecord(self, s):
        """
        :type s: str
        :rtype: bool
        """
        return False if "LLL" in s or s.count("A")>1 else True
Copy The Code & Try With Live Editor

Input

x
+
cmd
s = "PPALLL"

Output

x
+
cmd
false

#4 Code Example with C# Programming

Code - C# Programming


namespace LeetCode
{
    public class _0551_StudentAttendanceRecordI
    {
        public bool CheckRecord(string s)
        {
            int countA = 0, countL = 0;
            foreach (var ch in s)
            {
                if (ch == 'A')
                {
                    countA++;
                    countL = 0;
                }
                else if (ch == 'L')
                    countL++;
                else
                    countL = 0;
                if (countA > 1 || countL > 2) return false;
            }

            return true;
        }
    }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
s = "PPALLL"

Output

x
+
cmd
false
Advertisements

Demonstration


Previous
#547 Leetcode Number of Provinces Solution in C, C++, Java, JavaScript, Python, C# Leetcode
Next
#552 Leetcode Student Attendance Record II Solution in C, C++, Java, JavaScript, Python, C# Leetcode