
Alex is having a problem with his keyboard. If he presses any key, he may get extra occurrences of that character. Example: If he wants to write "hey", he may end up typing: hhey, hheey, heey, hhheyyy, etc.
You are given a string 'S' typed by Alex. Your task is to find out the number of possible strings Alex was trying to type and return the same.
Output can be very large. Return (number of ways % 1e9 + 7). Where % is the modular operator.
Let 'S' = "adddef".
Alex could have been trying to type:
- "adef" (second and third 'd' are extra)
- "addef" (second 'd' is extra)
- "addef" (third 'd' is extra)
- "adddef" (no extra characters)
So we have 3 distinct possibilities: "adef", "addef", and "adddef".
Therefore, the answer is 3.
The first line contains a string 'S'.
Output Format :
Return the number of possible original strings modulo 1e9 + 7.
Note :
You don't need to print anything. Just implement the given function.
1 <= |S| <= 10^5
'S' contains only lowercase English letters
Time Limit: 1 sec
aab
2
Alex typed "aab". The possible original strings he intended are:
- "ab" (second 'a' is extra)
- "aab" (no characters are extra)
So we have 2 distinct possibilities: "ab" and "aab".
Thus, the answer is 2.
aaabbc
6
For each group of consecutive identical characters, Alex could have intended any number from 1 to the total count of that character.
Approach:
Algorithm:
O(N), where 'N' is the length of string 'S'.
We iterate through each character in the string once to group consecutive identical characters. Thus, the overall time complexity is of the order O(N).
O(1).
We use only a constant amount of extra space for variables like 'count', 'result', and loop counters. Thus, the overall space complexity is of the order O(1).