802. Hidden Copies
A detective searches a long ransom note s for a keyword t. The keyword is spelled by picking letters of the note from left to right, not necessarily next to each other, so that the picked letters in order read exactly t.
Count the number of different ways to do this. Two ways are different when the sets of picked positions in s are different, even if they spell the same letters. Because the count can be huge, return it modulo 1000000007. If t is longer than s the answer is 0.
Aim for O(n*m) time, where n and m are the lengths of s and t, and O(m) extra space.
Example 1
- Input:
- s = "bananarama", t = "ana"
- Output:
- 10
- Explanation:
There are 10 different sets of positions in "bananarama" whose letters, read left to right, spell "ana".
Example 2
- Input:
- s = "zigzagzig", t = "zig"
- Output:
- 6
- Explanation:
There are 6 different sets of positions in "zigzagzig" whose letters, read left to right, spell "zig".
Example 3
- Input:
- s = "kkkkk", t = "kk"
- Output:
- 10
- Explanation:
There are 10 different sets of positions in "kkkkk" whose letters, read left to right, spell "kk".
Constraints
1 ≤ t.length ≤ 1000, 1 ≤ s.length ≤ 3000
Both strings contain only lowercase English letters.
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
- Time per case
- Python 1,200 msC++ 300 msJava 600 msJavaScript 600 msTypeScript 600 ms
Expected complexity
- Time
- O(n*m)
- Space
- O(m)