You are given a list of strings of the same lengthwords and a string target.
Your task is to form target using the given words under the following rules:
target should be formed from left to right.
To form the ith character (0-indexed) of target, you can choose the kth character of the jth string in words if target[i] = words[j][k].
Once you use the kth character of the jth string of words, you can no longer use the xth character of any string in words where x <= k. In other words, all characters to the left of or at index k become 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 modulo10^9 + 7.
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:
1
2
3
4
5
6
7
8
9
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 <= 1000
1 <= words[i].length <= 1000
All strings in words have the same length.
1 <= target.length <= 1000
words[i] and target contain only lowercase English letters.
Precompute the frequency of each character at each position in words, then use dynamic programming to count the number of ways to form the target string by choosing valid characters from left to right.
classSolution {
public:int numWays(vector<string>& words, string target) {
int MOD =1e9+7, m = words[0].size(), n = target.size();
vector<vector<int>> freq(26, vector<int>(m));
for (auto& w : words)
for (int i =0; i < m; ++i)
freq[w[i]-'a'][i]++;
vector<vector<long>> dp(n+1, vector<long>(m+1));
dp[0][0] =1;
for (int i =0; i < n; ++i) {
for (int j =0; j < m; ++j) {
if (dp[i][j] ==0) continue;
// skip column j
dp[i][j+1] = (dp[i][j+1] + dp[i][j]) % MOD;
// use column j if possible
int c = target[i]-'a';
if (freq[c][j] >0)
dp[i+1][j+1] = (dp[i+1][j+1] + dp[i][j] * freq[c][j]) % MOD;
}
}
long res =0;
for (int j =0; j <= m; ++j) res = (res + dp[n][j]) % MOD;
return res;
}
};
classSolution {
publicintnumWays(String[] words, String target) {
int MOD = 1_000_000_007, m = words[0].length(), n = target.length();
int[][] freq =newint[26][m];
for (String w : words)
for (int i = 0; i < m; ++i)
freq[w.charAt(i)-'a'][i]++;
long[][] dp =newlong[n+1][m+1];
dp[0][0]= 1;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if (dp[i][j]== 0) continue;
dp[i][j+1]= (dp[i][j+1]+ dp[i][j]) % MOD;
int c = target.charAt(i)-'a';
if (freq[c][j]> 0)
dp[i+1][j+1]= (dp[i+1][j+1]+ dp[i][j]* freq[c][j]) % MOD;
}
}
long res = 0;
for (int j = 0; j <= m; ++j) res = (res + dp[n][j]) % MOD;
return (int)res;
}
}
classSolution {
funnumWays(words: Array<String>, target: String): Int {
val MOD = 1_000_000_007
val m = words[0].length
val n = target.length
val freq = Array(26) { IntArray(m) }
for (w in words)
for (i in0 until m)
freq[w[i]-'a'][i]++val dp = Array(n+1) { LongArray(m+1) }
dp[0][0] = 1Lfor (i in0 until n) {
for (j in0 until m) {
if (dp[i][j] ==0L) continue dp[i][j+1] = (dp[i][j+1] + dp[i][j]) % MOD
val c = target[i]-'a'if (freq[c][j] > 0)
dp[i+1][j+1] = (dp[i+1][j+1] + dp[i][j] * freq[c][j]) % MOD
}
}
var res = 0Lfor (j in0..m) res = (res + dp[n][j]) % MOD
return res.toInt()
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
classSolution:
defnumWays(self, words: list[str], target: str) -> int:
MOD =10**9+7 m, n = len(words[0]), len(target)
freq = [[0]*m for _ in range(26)]
for w in words:
for i, c in enumerate(w):
freq[ord(c)-97][i] +=1 dp = [[0]*(m+1) for _ in range(n+1)]
dp[0][0] =1for i in range(n):
for j in range(m):
if dp[i][j] ==0: continue dp[i][j+1] = (dp[i][j+1] + dp[i][j]) % MOD
c = ord(target[i])-97if freq[c][j] >0:
dp[i+1][j+1] = (dp[i+1][j+1] + dp[i][j]*freq[c][j]) % MOD
return sum(dp[n][j] for j in range(m+1)) % MOD