Formatted question description: https://leetcode.ca/all/551.html

551. Student Attendance Record I (Easy)

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

  1. 'A' : Absent.
  2. 'L' : Late.
  3. '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

Related Topics:
String

Similar Questions:

Solution 1.

// OJ: https://leetcode.com/problems/student-attendance-record-i/

// Time: O(N)
// Space: O(1)
class Solution {
public:
    bool checkRecord(string s) {
        int A = 0, L = 0;
        for (char c : s) {
            if (c == 'A') ++A;
            if (c == 'L') ++L;
            else L = 0;
            if (A > 1 || L > 2) return false;
        }
        return true;
    }
};

Java

  • class Solution {
        public boolean checkRecord(String s) {
            int absents = 0, continuousLates = 0;
            int length = s.length();
            for (int i = 0; i < length; i++) {
                char c = s.charAt(i);
                if (c == 'L') {
                    continuousLates++;
                    if (continuousLates > 2)
                        return false;
                } else {
                    if (c == 'A') {
                        absents++;
                        if (absents > 1)
                            return false;
                    }
                    continuousLates = 0;
                }
            }
            return true;
        }
    }
    
  • // OJ: https://leetcode.com/problems/student-attendance-record-i/
    // Time: O(N)
    // Space: O(1)
    class Solution {
    public:
        bool checkRecord(string s) {
            int A = 0, L = 0;
            for (char c : s) {
                if (c == 'A') ++A;
                if (c == 'L') ++L;
                else L = 0;
                if (A > 1 || L > 2) return false;
            }
            return true;
        }
    };
    
  • class Solution(object):
      def checkRecord(self, s):
        """
        :type s: str
        :rtype: bool
        """
        a = l = 0
        for c in s:
          if c == "L":
            l += 1
          elif c == "A":
            a += 1
            l = 0
          else:
            l = 0
          if a > 1 or l > 2:
            return False
        return True
    
    

All Problems

All Solutions