This post is completed by 3 users

  • 0
Add to List
Beginner

522. Check If Student is eligible for an Attendance Reward

In school a student gets rewarded if he has an attendance record without being absent for more than once or being late for 3 times continuously. 

Given a student's attendance record represented by a string. The record only contains the following three characters:

  • 'A' : Absent.
  • 'L' : Late.
  • 'O' : On-Time.

Check whether the student qualifies for the reward.

Example:

Record: "OLLAOOOLLO"
Output: False
Explanation: The student does not qualify for a reward because "LLA" means he was late 3 times in a row.

Record: "OLLOAOLLO"
Output: True

Approach: 

Initialize prev = null, lateCount=0, absentCount=0. 

Iterate the array and 

  1. If the current character is 'A', increment the absentCount.  
  2. If the current character is either 'L' or 'A', check the prev character is not 'O' (means again prev character is either 'A' or 'L') then increment the lateCount. 
  3. Do prev = current character at the end of each iteration.
  4. If anytime absentCount>1 OR lateCount>2, return false.

Output:

Record: OLLAOOOLLO
Attendance reward: false

Reference: careercup