Given a string str. The task is to count the number of all different strings possible if two consecutive same characters of the string can be replaced by one different character.
Following properties were observed for replacing two equals characters at a time:
- If for string = “aaa” of length 3 we replace two “aa” with one character say “K” then the total number of different possible strings including the original string is:- Ka, aK, aaa. Therefore the number of different strings follows the property of Fibonacci Number of the length of the consecutive characters in the string.
- If for string = “aaadefyyyy” then the total number of possible different strings is equaled to the product of combinations of string “aaa” and “yyyy” by replacing the two consecutive characters at a time.
Hence from the above two observations, the count of different possible strings with N consecutive characters is given by N-th Fibonacci Number. Therefore, the total number of different possible strings for the given string str is equal to the product of count of different possible strings for every substring with all same character.
Following are the steps:
- Count(say cnt) the number of consecutive characters which are same in the given string str.
- To count the different possible strings for the count cnt, find the value of Fibonacci Sequence at cnt.
- Repeat the above steps for all the consecutive characters in the given string str.
- The total count of different possible strings is equals to the product of all the value of Fibonacci Sequence obtained for every count of consecutive characters.
Below is the implementation of the above approach:
Time Complexity: O(N), where N is the length of the given string.
- Find length of the longest consecutive path from a given starting character
- Longest Common Prefix using Character by Character Matching
- Modify the string such that every character gets replaced with the next character in the keyboard
- Sub-strings that start and end with one character and have at least one other
- Sub-strings of length K containing same character
- Print the most occurring character in an array of strings
- Program to build a DFA to accept strings that start and end with same character
- NFA to accept strings that has atleast one character occurring in a multiple of 3
- Program to build a DFA that accepts strings starting and ending with different character
- Total character pairs from two strings, with equal number of set bits in their ascii value
- Count of binary strings of length N with even set bit count and at most K consecutive 1s
- Count occurrences of a sub-string with one variable character
- Count of number of given string in 2D character array
- Index of character depending on frequency count in string
- Count of sub-strings with equal consecutive 0's and 1's
- Count number of binary strings without consecutive 1's
- Count number of binary strings without consecutive 1’s : Set 2
- Count of Binary strings of length N having atmost M consecutive 1s or 0s alternatively exactly K times
- Replacing an element makes array elements consecutive
- Count of integers obtained by replacing ? in the given string that give remainder 5 when divided by 13
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.