Student Attendance Record I Leetcode Solution

Difficulty Level Easy
Frequently asked in Google
algorithms coding Interview interviewprep LeetCode LeetCodeSolutions StringViews 4563

Problem statement

In the problem ” Student Attendance Record I” we are given a string where each letter represents the attendance detail of a student. The interpretation of letters in the string is as follows:

  1. ‘A’ means absent.
  2. ‘P’ means present.
  3. ‘L’ means late

The student will be rewarded based on his attendance if he is not absent for more than one day or not late continuously for more than two days. Our task is to determine if the student will be rewarded or not.

Example

str="PPALLP"
true

Explanation:

Student Attendance Record I Leetcode Solution

As the student is not absent for more than one day and not late continuously for more than two days so he must be rewarded.

Approach for Student Attendance Record I Leetcode Solution

Implementation

This is a basic implementation problem. We need to find out if the student will be rewarded or not. So to reward the student we need to check if he is not absent for more than one day and not late continuously for more than two days. We will follow these steps to perform this check:

  1. Initialize number of ‘A’ count and ‘L’ count by zero
  2. Traverse the complete string.
    1. If the current character is ‘A’ then increment the count of ‘A’ by one.
    2. If the current character is ‘L’ then increment the count of ‘L’ by one.
    3. Otherwise, the count of ‘L’ becomes zero.
    4. Now check if the count of ‘A’ is greater than or equal to 2 or the count of ‘L’ is greater than 2. If the condition is true return false.
  3. In the end, if all conditions are satisfied then return true.

C++ code for Student Attendance Record I

#include <bits/stdc++.h> 
using namespace std; 
bool checkRecord(string s) {
    int a=0, l=0;
    for(int i=0;i<s.size();i++) {
        if(s[i]=='A') a++;
        if(s[i]=='L') l++;
        else l=0;
        if(a>=2||l>2) return false;
    }
    return true;
}

int main() 
{ 
    string attendence ="PPALLP"; 
    bool ans=checkRecord(attendence); 
    cout<<boolalpha;
    cout<<ans<<endl;
    return 0;
}
true

Java code for Student Attendance Record I

import java.util.Arrays;
import java.util.Set ;
import java.util.HashSet;
import java.util.*; 
public class Tutorialcup {
    public static boolean checkRecord(String s) {
    int a=0, l=0;
    for(int i=0;i<s.length();i++) {
        if(s.charAt(i)=='A') a++;
        if(s.charAt(i)=='L') l++;
        else l=0;
        if(a>=2||l>2) return false;
    }
    return true; 
    }
  public static void main(String[] args) {
        String attendence ="PPALLP"; 
        boolean ans=checkRecord(attendence); 
        System.out.println(ans);
  }
}
true

Complexity Analysis of Student Attendance Record I Leetcode Solution

Time complexity

The time complexity of the above code is O(n) because we are traversing the string only once. Here n is the length of the given string.

Space complexity

The space complexity of the above code is O(1) because we are using only a variable to store answer.

References

Translate »