# Leetcode – Student Attendance Record I Solution in Python

## Introduction

The Student Attendance Record I problem on LeetCode is an engaging string processing challenge that demands a good understanding of string manipulation and iterative techniques in Python. This problem is a prime example of a scenario where elementary programming concepts can be applied to simulate real-world scenarios. In this in-depth article, we will dissect the Student Attendance Record I problem, understand its underlying logic, and explore various approaches to solve it using Python.

The problem is described as follows:

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

• ‘A’ : Absent.
• ‘L’ : Late.
• ‘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.

For example:

Input: s = "PPALLP"
Output: True

Input: s = "PPALLL"
Output: False

## Approach 1: Iterative Counting

One straightforward approach to solving this problem is to iterate through the string and keep track of the number of ‘A’s and consecutive ‘L’s. If at any point, we find more than one ‘A’ or more than two consecutive ‘L’s, we can immediately return False, as the student cannot be rewarded.

Here’s the Python code for this approach.

def checkRecord(s):
# Count the number of 'A's
a_count = 0

# Count consecutive 'L's
l_count = 0

# Iterate through the string
for char in s:
# Increment a_count if 'A' is found
if char == 'A':
a_count += 1
# Reset l_count
l_count = 0

# Increment l_count if 'L' is found
elif char == 'L':
l_count += 1

# Reset l_count if any other character is found
else:
l_count = 0

# Check if conditions are violated
if a_count > 1 or l_count > 2:
return False

# If the loop completes, the record is rewardable
return True

This approach has a time complexity of O(n), where n is the length of the string.

## Approach 2: Using Python Built-In Functions

Python’s built-in functions and string methods can be employed for a more succinct solution. We can use the count method to check for more than one ‘A’, and the in operator to check for three consecutive ‘L’s.

Here’s the Python code for this approach.

def checkRecord(s):
# Check if the string contains more than one 'A' or three consecutive 'L's
return s.count('A') <= 1 and 'LLL' not in s

This approach is also O(n) in time complexity but is more concise and leverages Python’s expressive syntax and built-in functions.

## Testing the Solutions

Let’s test the solutions with some examples.

# Test Case 1
print(checkRecord("PPALLP"))  # Output should be True

# Test Case 2
print(checkRecord("PPALLL"))  # Output should be False

# Test Case 3
print(checkRecord("PAPLP"))   # Output should be True

# Test Case 4
print(checkRecord("PAPLPLPLPLLLLP"))  # Output should be False

## Conclusion

In this detailed article, we investigated the Student Attendance Record I problem on Leetcode. We analyzed the problem, and explored two distinct approaches to solve it: an iterative counting approach, and a concise approach that employs Python’s built-in functions. This problem serves as a good example to understand the value of understanding built-in functions in Python and also demonstrates how a seemingly simple problem can be solved in multiple ways. Additionally, it is an instance that models real-world scenarios, showing the practical application of programming concepts in everyday life.