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

Note: This problem is from LeetCode.
❌ Compiled
❌ Executed
❌ Correct
program main
    implicit none

    character(len=1000) :: s
    logical :: result

    ! Example 1
    s = "PPALLP "
    write(*,*) solve(s), "Expected: True"

    ! Example 2
    s = "PPALLL "
    write(*,*) solve(s), "Expected: False"

contains

    function solve(s) result(result)
        implicit none
        character(len=*), intent(in) :: s
        integer :: i, j, k, n_absent, n_late
        logical :: is_eligible

        n_absent = 0
        n_late = 0
        is_eligible = .false.

        do i = 1, len(s) - 2
            if (s(i:i+2) == 'AA') then
                n_absent = n_absent + 1
            end if
            if (s(i:i+2) == 'LLL') then
                n_late = n_late + 1
            end if
            if (n_absent < 2 .and. n_late == 0) then
                is_eligible = .true.
            else if (n_absent >= 2 .or. n_late >= 3) then
                exit
            end if
        end do

        result = is_eligible
    end function solve

end program main
βœ… Compiled
❌ Executed
❌ Correct
! Declare variables
integer :: i, j, k, n, m, count_absences, count_consecutive_late
character(len=1000) :: s

! Read input
read(*,*) s

! Initialize variables
n = len(s)
m = 0
count_absences = 0
count_consecutive_late = 0

! Loop through the string
do i = 1, n
    if (s(i:i) == 'A') then
        count_absences = count_absences + 1
    else if (s(i:i) == 'L') then
        count_consecutive_late = count_consecutive_late + 1
    else
        count_consecutive_late = 0
    end if

    ! Check if the student is eligible for the award
    if (count_absences < 2 .and. count_consecutive_late == 0) then
        write(*,*) "true"
        return
    end if
end do

! If the student is not eligible for the award, print "false"
write(*,*) "false"

end
🌐 Data from online sources
def checkRecord(s: str) -> bool:
    late, absent = 0, 0
    for c in s:
        if c == 'A':
            absent += 1
            late = 0
        elif c == 'L':
            late += 1
        else:
            late = 0

        if absent >= 2 or late >= 3:
            return False
    return True

The algorithm initializes variables late and absent as counters for late and absent days, respectively. 1. Iterate through each character of the string s. 2. If the current character is 'A', increment the absent counter and set late counter to zero. 3. If the current character is 'L', increment the late counter. 4. If the current character is 'P', set the late counter to zero. 5. After processing each character, check if absent is greater or equal to 2, or if late is greater or equal to 3. If either condition is met, return false (ineligible for an attendance award). 6. If the entire string has been processed without meeting the conditions, return true (eligible for an attendance award).

🌐 Data from online sources
#include <string>

bool checkRecord(std::string s) {
    int late = 0, absent = 0;
    for (int i = 0; i < s.length(); i++) {
        if (s[i] == 'A') {
            absent++;
            late = 0;
        } else if (s[i] == 'L') {
            late++;
        } else {
            late = 0;
        }

        if (absent >= 2 || late >= 3) return false;
    }
    return true;
}

The algorithm initializes variables late and absent as counters for late and absent days, respectively. 1. Iterate through each character of the string s. 2. If the current character is 'A', increment the absent counter and set late counter to zero. 3. If the current character is 'L', increment the late counter. 4. If the current character is 'P', set the late counter to zero. 5. After processing each character, check if absent is greater or equal to 2, or if late is greater or equal to 3. If either condition is met, return false (ineligible for an attendance award). 6. If the entire string has been processed without meeting the conditions, return true (eligible for an attendance award).