

Subsequences are created by deleting 0 or more characters from a sequence without changing the order.
Let ‘PATH’ be: "rrr"
Let ‘X’ = 1, ‘Y’ = 2, and ‘R’ = 10
Paths are ["r", "r", "r"]. As we have to choose distinct subsequences, so the result is 1.
The first line of input contains an integer ‘T’, the number of test cases.
The first line of each test case contains a string ‘PATH’, representing the sequence of moves.
The second of each test case contains three space-separated integers ‘X’, ‘Y’, and ‘R’, representing the start position, end position, and the range of number line.
For each test case, print a single integer representing the number of distinct subsequences modulo (10^9 + 7).
Print output of each test case in a separate line.
You do not need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= |PATH| <= 10^3
0 <= X, Y < R <= 2500
'PATH' contains only two characters 'r' and 'l'.
Time Limit: 1 sec
The basic idea is to check whether it is possible to reach from position ‘X’ to ‘Y’ using all possible subsequences of the moves. We also use a set to count only the distinct subsequences.
Here is the algorithm :
HELPER(PATH, X, Y, R, IDX, S, TEMP) (where ‘PATH’, ‘X’, ‘Y’, AND ‘R’ are given variables, ‘IDX’ is the current index of string, ‘S’ the set, ‘TEMP’ is the current subsequence.)
CHECK(TEMP, X, Y, R) (where ‘TEMP’ is the string to be checked, ‘X’, ‘Y’, and ‘R’ are given variables.)
The basic idea is to use memoization to reduce the recursive calls. We have two options while creating a subsequence, i.e., to pick a current character or ignore it. The duplicates arise because while considering a subsequence, we ignore a character and then pick that same character in the next move. We create a ‘DP’ array to avoid this.
We define ‘DP[IDX][X][FLAG] that represents the count of unique subsequences of ‘PATH[IDX]’ that starts from position ‘X’, and ‘FLAG’ represents the previous direction we moved.
We can have four different choices for ‘FLAG’:
Here is the algorithm :
HELPER(PATH, IDX, FLAG, X, Y, R) (where ‘PATH’, ‘X’, ‘Y’, and ‘R’ are given variables, ‘IDX’ is the current index of string, ‘FLAG’ represents the previous direction taken).