Problem · Dynamic Programming
Distinct Moves
Learn this problemProblem statement
Given a number line with positions labeled from 0 to n, and a sequence of movements consisting of instructions 'l' (move left by 1) and 'r' (move right by 1), determine how many distinct subsequences of these moves will take you from a starting position x to an ending position y. Return the result modulo (10^9 + 7).
Notes:
Function
distinctMoves(s: String, n: int, x: int, y: int) → int
Complete the function distinctMoves in the editor with the following parameter(s):
- string s: the sequence of moves
- int n: the upper bound of the number line
- int x: the starting point
- int y: the ending point
Returns
int: the number of distinct subsequences modulo (10^9+7)
A huge thanks to the friend who shared the source! 🙏
Examples
Example 1
s = "rrlrlr"n = 6x = 1y = 2return = 7
Part of the image explanation:
Constraints
- 1 ≤ |s| ≤ 10^3
- 0 ≤ x,y,n ≤ 2500