Number of Ways to Form a Target String Given a Dictionary Problem
Description
LeetCode Problem 1639.
You are given a list of strings of the same length words and a string target.
Your task is to form target using the given words under the following rules:
targetshould be formed from left to right.- To form the
i^thcharacter (0-indexed) oftarget, you can choose thek^thcharacter of thej^thstring inwordsiftarget[i] = words[j][k]. - Once you use the
k^thcharacter of thej^thstring ofwords, you can no longer use thex^thcharacter of any string inwordswherex <= k. In other words, all characters to the left of or at indexkbecome unusuable for every string. - Repeat the process until you form the string
target.
Notice that you can use multiple characters from the same string in words provided the conditions above are met.
Return the number of ways to form target from words. Since the answer may be too large, return it modulo 10^9 + 7.
Example 1:
Input: words = ["acca","bbbb","caca"], target = "aba"
Output: 6
Explanation: There are 6 ways to form target.
"aba" -> index 0 ("acca"), index 1 ("bbbb"), index 3 ("caca")
"aba" -> index 0 ("acca"), index 2 ("bbbb"), index 3 ("caca")
"aba" -> index 0 ("acca"), index 1 ("bbbb"), index 3 ("acca")
"aba" -> index 0 ("acca"), index 2 ("bbbb"), index 3 ("acca")
"aba" -> index 1 ("caca"), index 2 ("bbbb"), index 3 ("acca")
"aba" -> index 1 ("caca"), index 2 ("bbbb"), index 3 ("caca")
Example 2:
Input: words = ["abba","baab"], target = "bab"
Output: 4
Explanation: There are 4 ways to form target.
"bab" -> index 0 ("baab"), index 1 ("baab"), index 2 ("abba")
"bab" -> index 0 ("baab"), index 1 ("baab"), index 3 ("baab")
"bab" -> index 0 ("baab"), index 2 ("baab"), index 3 ("baab")
"bab" -> index 1 ("abba"), index 2 ("baab"), index 3 ("baab")
Constraints:
1 <= words.length <= 10001 <= words[i].length <= 1000- All strings in
wordshave the same length. 1 <= target.length <= 1000words[i]andtargetcontain only lowercase English letters.
Solution
Approach: Column Counts + Dynamic Programming
The rule "once column k is used, nothing at or left of k can be used" means that target's characters come from strictly increasing columns. Within one column it does not matter which word we take the letter from, only how many words have the right letter there. So count cnt[j][c]: the number of words with letter c in column j.
Let f[i][j] be the number of ways to build the first i characters of target using only columns 0..j-1. For column j-1 there are two choices:
- do not use it:
f[i][j-1]ways; - use it for
target[i-1]:f[i-1][j-1] * cnt[j-1][target[i-1]]ways, one for each word that has that letter in that column.
So f[i][j] = f[i][j-1] + f[i-1][j-1] * cnt[j-1][target[i-1]], modulo 10^9 + 7. The empty prefix can be built in one way, so f[0][j] = 1 for every j, and the answer is f[m][n]. If target is longer than the words, it simply comes out as 0.
The product can reach about 1012, so use 64-bit integers for it in Java, C++ and Go.
Complexity: O(w · n + m · n) time, where w is the number of words, n their length and m the length of target. O(m · n) space for the table (one row at a time would also do).
class Solution:
def numWays(self, words: List[str], target: str) -> int:
mod = 10**9 + 7
m, n = len(target), len(words[0])
# cnt[j][c] = how many words have letter c in column j
cnt = [[0] * 26 for _ in range(n)]
for w in words:
for j, c in enumerate(w):
cnt[j][ord(c) - ord('a')] += 1
# f[i][j] = ways to build target[:i] using only columns 0..j-1
f = [[0] * (n + 1) for _ in range(m + 1)]
f[0] = [1] * (n + 1)
for i in range(1, m + 1):
c = ord(target[i - 1]) - ord('a')
for j in range(1, n + 1):
# Either skip column j-1, or take target[i-1] from it (any word that has it there).
f[i][j] = (f[i][j - 1] + f[i - 1][j - 1] * cnt[j - 1][c]) % mod
return f[m][n]