551. Student Attendance Record I

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

1. 'A' : Absent.
2. 'L' : Late.
3. '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.

Example 1:

```Input: "PPALLP"
Output: True
```

Example 2:

```Input: "PPALLL"
Output: False
```

b'
\n\n

Solution

\n
\n

Approach #1 Simple Solution [Accepted]

\n

One simple way of solving this problem is to count number of in the string and check whether the string is a substring of a given string. If number of is less than and is not a subtring of a given string then return , otherwise return .

\n

\n method can be used to check substring in a string. It return the index within this string of the first occurrence of the specified character or -1, if the character does not occur.

\n\n

Complexity Analysis

\n
\n
• Time complexity : . Single loop and indexOf method takes time.
• \n
• Space complexity : . Constant space is used.
• \n
\n
\n

Approach #2 Better Solution [Accepted]

\n

Algorithm

\n

One optimization of above method is to break the loop when count of becomes .

\n\n

Complexity Analysis

\n
\n
• Time complexity : . Single loop and indexOf method takes time.
• \n
• Space complexity : . Constant space is used.
• \n
\n
\n

Approach #3 Single pass Solution (Without indexOf method) [Accepted]

\n

Algorithm

\n

We can solve this problem in a single pass without using indexOf method. In a single loop we can count number of and also check the substring in a given string.

\n\n

Complexity Analysis

\n
\n
• Time complexity : . Single loop upto string length is used.
• \n
• Space complexity : . Constant space is used.
• \n
\n
\n

Approach #4 Using Regex [Accepted]

\n

Algorithm

\n

One interesting solution is to use regex to match the string. Java provides the java.util.regex package for pattern matching with regular expressions. A regular expression is a special sequence of characters that helps you match or find other strings or sets of strings, using a specialized syntax held in a pattern.

\n

Following are the regex\'s used in this solution:

\n
`. : Matches any single character except newline.\n\n* : Matches 0 or more occurrences of the preceding expression.\n\n.* : Matches any string\n\na|b : Matches either a or b\n`
\n

\n method is used to check whether or not the string matches the given regular expression.

\n

Regular Expression of the string containing two or more than two will be and the regular expression of the string containing substring will be . We can merge this two regex using and form a regex of string containing either more than one or containing substring . Then regex will look like: . We will return true only when the string doesn\'t matches this regex.

\n\n

Complexity Analysis

\n
\n
• \n

Time complexity : . method takes time.

\n
• \n
• \n

Space complexity : . No Extra Space is used.

\n
• \n
\n
\n

Analysis written by: @vinod23

\n
'