![]() |
VOOZH | about |
Given a string str. A faulty machine prints bb instead of character a and prints dd instead of char c and for the rest characters it prints normally, the task is to find how many distinct original strings could result in the given string. Since the answer can be large print ans modulo 1e9+7.
Examples:
Input: str = "gfgbbndd"
Output: 4
Explanation: Following strings result in str when the faulty machine prints it.
- gfgandd -> gfgbbndd
- gfganc -> gfgbbndd
- gfgbbnc -> gfgbbndd
- gfgbbndd -> gfgbbndd
Approach: To solve the problem follow the below idea:
We can use Dynamic Programming here, whenever we encounter double b (bb ) or double d (dd) we can generate 2 strings
Below is the implementation of above idea:
4
Time Complexity: O(N)
Auxiliary Space: O(N)