894. Spell From Columns
A signmaker owns words, several equally long strips of letter tiles, and wants to build the word target from them. Letters of target are chosen from left to right: the character at index i of target is copied from some strip at some column c, and the next character must come from a strictly larger column than the previous one. Any strip may be used for any letter, and strips and columns can be reused across different positions of target as long as the column rule holds.
Two ways are different if some letter of target is taken from a different strip or a different column. Return the number of ways to build target, modulo 10^9 + 7.
Example 1
- Input:
- words = ["tile","lite","tote"], target = "tie"
- Output:
- 12
- Explanation:
The t comes from column 0 of two strips, the i from column 1 of two strips and the e from column 3 of all three: 2 * 2 * 3 = 12 (a t from column 2 leaves no later i).
Example 2
- Input:
- words = ["aaa","aba"], target = "ab"
- Output:
- 2
- Explanation:
The b exists only in column 1 of the second strip; the a before it can come from column 0 of either strip: 2 ways.
Example 3
- Input:
- words = ["xyz","zzz"], target = "xx"
- Output:
- 0
- Explanation:
The letter x appears only in column 0, so two x letters cannot use increasing columns: 0 ways.
Constraints
- 1 ≤ words.length ≤ 1000
- All strips have the same length L with 1 ≤ L ≤ 1000
- 1 ≤ target.length ≤ 1000
- All strings consist of 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 600 msC++ 150 msJava 300 msJavaScript 300 msTypeScript 300 ms
Expected complexity
- Time
- O(L * (n + T))
- Space
- O(T)