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:
'A') for strictly fewer than 2 days total.'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 <= 1000s[i] is either 'A', 'L', or 'P'.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach to this attendance record problem involves checking every possible attendance record to see if it violates the rules. This means we will generate every combination of 'A', 'L', and 'P' for the given length and validate each one.
Here's how the algorithm would work step-by-step:
def check_record_brute_force(length_of_record):
valid_record_count = 0
possible_characters = ['A', 'L', 'P']
def generate_attendance_records(current_record, current_length):
nonlocal valid_record_count
# Base case: record is long enough
if current_length == length_of_record:
absent_count = current_record.count('A')
# Check for at most one absent
if absent_count <= 1:
# Check for no three consecutive late days
if 'LLL' not in current_record:
valid_record_count += 1
return
# Recursive generation of strings
for character in possible_characters:
generate_attendance_records(current_record + character, current_length + 1)
generate_attendance_records('', 0)
return valid_record_countThe goal is to determine if a student's attendance record is acceptable based on certain criteria. We need to check for too many absences or consecutive late days by scanning the record once.
Here's how the algorithm would work step-by-step:
def check_record(attendance_record):
number_of_absences = 0
consecutive_late_days = 0
max_consecutive_late_days = 0
for record in attendance_record:
if record == 'A':
number_of_absences += 1
# More than one absence makes the record invalid.
if number_of_absences >= 2:
return False
consecutive_late_days = 0
elif record == 'L':
consecutive_late_days += 1
# Keep track of the longest streak of consecutive Lates.
max_consecutive_late_days = max(max_consecutive_late_days, consecutive_late_days)
# Three or more consecutive lates makes the record invalid.
if max_consecutive_late_days >= 3:
return False
else:
consecutive_late_days = 0
return True| Case | How to Handle |
|---|---|
| Null or empty input string | Return true if the input is null or empty, as an empty record technically satisfies the criteria of at most one absent and at most two consecutive late days. |
| String with a single character 'A' | Return false as it has one absent which is allowed, but a single A means the student can attend. |
| String with a single character 'L' | Return true as it has one late which is within limits |
| String with a single character 'P' | Return true as it has one present which is within limits |
| String with two consecutive 'L's | Return true, as it satisfies the criteria. |
| String with three consecutive 'L's | Return false, as it violates the maximum two consecutive late days rule. |
| String with two 'A's | Return false, as it violates the maximum one absent day rule. |
| Very long string with alternating 'P' and 'L' | The solution should iterate through the string only once, ensuring linear time complexity even with very large inputs. |