Given the length N of the string, we have to find the number of special strings of length N.
A string is called a special string if it consists only of lowercase letters a and b and there is at least one b between two a’s in the string. Since the number of strings may be very large, therefore print it modulo 10^9+7.
Input: N = 2
The number of special string so length 2 are 3 i.e. “ab”, “ba”, “bb”
Input: N = 3
The number of special string so length 3 are 5 i.e. “abb”, “aba”, “bab”, “bba”, “bbb”
To solve the problem mentioned above, the first observation is if the integer N is 0 then there can only be an empty string as the answer, if N is 1 then there can be two string “a” or “b” as an answer but if the value of N is greater than 1 then the answer is equal to the sum of previous two terms. Now to find the count of special strings we run a loop and for each integer i count of the special string of length i is equal to the sum of the count of special strings of length i-1 and count of special strings of length i-2. Store the value of each integer in an array and return the required answer.
Below is the implementation of the above approach:
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Count number of binary strings such that there is no substring of length greater than or equal to 3 with all 1's
- Count of same length Strings that exists lexicographically in between two given Strings
- Count of binary strings of length N with even set bit count and at most K consecutive 1s
- Count number of Special Set
- Count the number of special permutations
- Count of sub-strings of length n possible from the given string
- Count of binary strings of given length consisting of at least one 1
- Count ways to increase LCS length of two strings by one
- Count of non-palindromic strings of length M using given N characters
- Maximum count of sub-strings of length K consisting of same characters
- Count of Binary strings of length N having atmost M consecutive 1s or 0s alternatively exactly K times
- Number of binary strings such that there is no substring of length ≥ 3
- Number of strings of length N with no palindromic sub string
- Number of Binary Strings of length N with K adjacent Set Bits
- Find the number of binary strings of length N with at least 3 consecutive 1s
- Count number of distinct substrings of a given length
- Count number of strings (made of R, G and B) using given combination
- Count the number of common divisors of the given strings
- Count number of binary strings without consecutive 1’s : Set 2
- Count number of binary strings without consecutive 1's
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.