163. Counting Ways to Build a Word
Hard
A text tool needs to determine how many ways one word can be formed from another without changing the letter order. Each way corresponds to choosing distinct positions in s; different position choices count separately, even when they produce the same sequence of letters.
Given the strings s and t, return the number of ways to choose positions in s that form exactly t, in increasing index order. Return the result modulo 1,000,000,007.
Example 1
Input: s = "babgbag", t = "bag"
Output: 5
Explanation: There are five different position choices that form "bag".
Example 2
Input: s = "rabbbit", t = "rabbit"
Output: 3
Explanation: The three occurrences of the letter "b" in `s` allow three different position choices.
Example 3
Input: s = "abc", t = ""
Output: 1
Explanation: The empty string can be formed in exactly one way: by choosing no positions.
Constraints
- 0 <= s.length <= 2000
- 0 <= t.length <= 2000
- `s` and `t` contain only lowercase English letters.
Dynamic programmingStrings
Saved in this browser
Caso 1
s =
"babgbag"
t =
"bag"
Expected =
5
Caso 2
s =
"rabbbit"
t =
"rabbit"
Expected =
3
Caso 3
s =
"abc"
t =
""
Expected =
1
Run checks these 3 cases. Submit runs all 12, including hidden ones.