
Since the answer can be very large print the answer modulo 10 ^ 9 + 7.
Given:
‘S’ = OORROR.
The answer will be two since 2 “ROR” can be formed from the index (2,4,5), and (3,4,5) (0-based indexing).
The first line of input contains an integer ‘T’ denoting the number of test cases.
The next ‘T’ line contains a string ‘S,’ containing capital letters.
For each test case, You are supposed to return an integer that denotes the total number of times “ROR” occurs as a subsequence.
You are not required to print the expected output; it has already been taken care of. Just implement the function.
1 <= ‘T’ <= 10
1 <= ‘N’ <= 10^4
Time Limit: 1sec.
The idea is to use recursion and match “ROR” in our string ‘S’. If we match at some index, then we recur on the rest of the substring of “ROR” and ‘S’, but at the same time also recur for the same position of “ROR” in ‘S’.
The steps are as follows:
The idea is to use recursion and match “ROR” in our string ‘S’, if we match at some index, then we recur on the rest of the substring of “ROR” and ‘S’, but at the same time also recur for the same position of “ROR” in ‘S’.
Since there will exist repeating sub-problems, we can hash them using a 2-D ‘dp’ array where ‘dp[i][j]’ tells the number of matches till ‘i’th index in ‘S’ and ‘j’th index in ‘T’.
The steps are as follows:
To find the number of “ROR” subsequences in the given string, observe for each ‘O’ if we know a number of ‘R’ before and after it. Then the number of “ROR” subsequences for that ‘O’ is equal to the product of the number of ‘R’ before and after that ‘O’.
So, the idea is to maintain a variable to store the number of ‘R’ before index I, if the ith character of the string is ‘O’ and the number of ‘O’ before index I if the ith character is ‘R’.
The steps are as follows: