# Student Attendance Record I

Woke up today at 04:20 AM. It was raining. Spent some time reading and on this blog and at 06:00 time to make the donuts (actually breakfast).

Since I had not much time I did a search on LeetCode problems using the string “recursion”. Problem 551. Student Attendance Record I came up. I did not see the need to use recursion and the problem does not specify to use it either.

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

As you can see we are given some type of attendance for a student. The letters in the string have specific meanings. We need to populate a function that will parse the string and based on the specified rules determine if the student should be rewarded for attendance. Make sure you read and understand the rules that allow a student to be rewarded. I interpreted one way and that allowed me to pass the two sample tests. After I submitted the code it failed. I made a simple change and the solution was accepted.

```PPALLP

main <<< reward: true

PPALLL

main <<< reward: false

LLPPPLL

main <<< reward: true
```

In the first case the student has one absence and one set of two consecutive late days. The student will be rewarded. In the second example the student has a single absence and three consecutive late days. The student will not be rewarded. The third example has no absence and two instances of two consecutive late days.

```public boolean checkRecord(String s) {
}
```

This is the function we need to implement. It takes a string with the student attendance and should return true for reward or false for skipping it.

```    /**
* Test scaffolding.
*/
public static void main(String[] args) {

// **** open scanner ****
Scanner sc = new Scanner(System.in);

// **** read input string ****
String s = sc.nextLine();

// **** close scanner ****
sc.close();

// **** compute and display result ****
System.out.println("main <<< reward: " + checkRecord(s));
}
```

The test scaffolding for the problem is provided by LeetCode. In my case I like to develop the test scaffolding because I like to code and test on my machine using my preferred tools.

The test scaffolding is quite simple. Read the attendance string, pass it to the function we need to implement and display the result. For the problem we only need to develop the checkRecord() function.

```    /**
* Funcion that evaluates the data and returns the result.
*/
static boolean checkRecord(String s) {

// **** count number of "A" (absence) ****
int count = countPattern("A", s);

// **** check if student has more than 1 absence ****
if (count > 1)
return false;

// **** count 'LL' (consecutive lates) ****
count = countPattern("LLL", s);

// **** more than 2 ****
if (count != 0)
return false;

// **** reward student ****
return true;
}
```

I first start by counting the string “A” pattern in the attendance string. According to the requirement, if a student has more than one absence, there would be no reward. Then we repeat the process with a different pattern. Note that the pattern “LLL” is the one we need to eliminate the reward, so one or more occurrences would disqualify the student.

```    /**
* Count the occurances of the specified pattern in the
* specified strng.
*/
static int countPattern(String pattern, String s) {

// **** ****
int count       = 0;
int fromIndex   = 0;

// **** count the pattern ****
while ((fromIndex = s.indexOf(pattern, fromIndex)) != -1) {
count++;
fromIndex++;
}

// **** return count ****
return count;
}
```

This function nicely encapsulates the process of getting the number of times the specified pattern is found in the specified string. Hopefully it is clean and simple to follow.

The entire code for this project can be found in my GitHub repository.

If you have comments or questions regarding this, or any other post in this blog, or if you would like for me to serve of assistance with any phase in the SDLC (Software Development Life Cycle) of a project associated with a product or service, please do not hesitate and leave me a note below. If you prefer, send me a private message using the following address:  john.canessa@gmail.com. I will reply as soon as possible.

Keep on reading and experimenting. It is the best way to learn, become proficient, refresh your knowledge and enhance your developer toolset!

One last thing, many thanks to all 1,725 subscribers to this blog!!!

Keep safe during the COVID-19 pandemic and help restart the world economy.

John