Count Distinct Ways to Form Target from Two Strings Problem
Description
LeetCode Problem 3981.
You are given three strings word1, word2, and target.
Your task is to count the number of ways to form target by choosing characters from word1 and word2 under the following conditions:
- For each character of
target, choose one matching character from eitherword1orword2. - The chosen indices from
word1must be strictly increasing. - The chosen indices from
word2must be strictly increasing. - At least one character must be chosen from both
word1andword2.
Two ways are considered different if, for at least one position in target, the chosen character comes from a different string or a different index.
Return the number of ways. Since the answer may be very large, return it modulo 10^9 + 7.
Example 1:
Input: word1 = "abc", word2 = "bac", target = "abc"
Output: 5
Explanation:
There are 5 ways to form target:
- word1[0] = 'a', word1[1] = 'b', word2[2] = 'c'
- word1[0] = 'a', word2[0] = 'b', word1[2] = 'c'
- word1[0] = 'a', word2[0] = 'b', word2[2] = 'c'
- word2[1] = 'a', word1[1] = 'b', word1[2] = 'c'
- word2[1] = 'a', word1[1] = 'b', word2[2] = 'c'
All ways preserve the increasing index order inside each string and choose at least one character from each string.
Example 2:
Input: word1 = "cd", word2 = "cd", target = "ccd"
Output: 4
Explanation:
There are 4 ways to form target:
- word1[0] = 'c', word2[0] = 'c', word1[1] = 'd'
- word1[0] = 'c', word2[0] = 'c', word2[1] = 'd'
- word2[0] = 'c', word1[0] = 'c', word1[1] = 'd'
- word2[0] = 'c', word1[0] = 'c', word2[1] = 'd'
The first two 'c' characters in target must come one from each string. The final 'd' can be chosen from either string.
Example 3:
Input: word1 = "xy", word2 = "xy", target = "xyxy"
Output: 2
Explanation:
There are 2 ways to form target:
- word1[0] = 'x', word1[1] = 'y', word2[0] = 'x', word2[1] = 'y'
- word2[0] = 'x', word2[1] = 'y', word1[0] = 'x', word1[1] = 'y'
Each "xy" part in target comes entirely from one string.
Example 4:
Input: word1 = "ab", word2 = "cde", target = "ace"
Output: 1
Explanation:
The only way is to choose word1[0] = 'a', word2[0] = 'c', and word2[2] = 'e'. Thus, the answer is 1.
Constraints:
1 <= word1.length, word2.length, target.length <= 100word1,word2, andtargetconsist of lowercase English letters only.
Solution
Approach: Dynamic programming over the last used indices
Let dp[i][j] be the number of ways to build the current prefix of target when the last index taken from word1 is i and the last index taken from word2 is j (1-based, and 0 means nothing has been taken from that word yet). Start with dp[0][0] = 1.
For the next character c of target there are two kinds of moves. Taking it from word1 at index i' requires word1[i'] = c and moves from any state (i, j) with i < i' to (i', j). Taking it from word2 works the same way on the second coordinate. For a fixed j, the sum over all i < i' is a running prefix sum, so each layer costs O(|word1| * |word2|).
After all characters of target are placed, the answer is the sum of dp[i][j] over i >= 1 and j >= 1, which guarantees that both words were used at least once.
Complexity: time O(|target| * |word1| * |word2|), space O(|word1| * |word2|).
class Solution:
def interleaveCharacters(self, word1: str, word2: str, target: str) -> int:
mod = 10**9 + 7
p, q = len(word1), len(word2)
# dp[i][j]: ways where the last index used in word1 is i and in word2 is j (1-based, 0 = none yet)
dp = [[0] * (q + 1) for _ in range(p + 1)]
dp[0][0] = 1
for c in target:
nd = [[0] * (q + 1) for _ in range(p + 1)]
for j in range(q + 1):
s = 0
for i in range(1, p + 1):
s = (s + dp[i - 1][j]) % mod
if word1[i - 1] == c:
nd[i][j] = (nd[i][j] + s) % mod
for i in range(p + 1):
s = 0
for j in range(1, q + 1):
s = (s + dp[i][j - 1]) % mod
if word2[j - 1] == c:
nd[i][j] = (nd[i][j] + s) % mod
dp = nd
ans = 0
for i in range(1, p + 1):
for j in range(1, q + 1):
ans += dp[i][j]
return ans % mod