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.
Input: "PPALLP" Output: True
Input: "PPALLL" Output: False
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
One optimization of above method is to break the loop when count of becomes .\n\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
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 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
Time complexity : . method takes time.\n
Space complexity : . No Extra Space is used.\n
Analysis written by: @vinod23\n