Given three strings p, q, r. Count the possible number of ways to create string r using string p and string q such that, the order of the selected characters in all the strings is preserved and at least one character from both the strings (p and q) is selected.
Note
• Strings will have max length 100
• They will consist of lowercase english alphabets.
. Return the answer modulo 10^9 +7.
Sample test case
p: ab
q: ba
r: aba
output: 2
Two ways to form aba
• from p, a, from q, ba
. from p, ab, from q,a
Assistant
To solve this problem, we need to count the number of ways to form string r by interleaving characters from strings p and q while preserving the order of characters in both p and q. Additionally, at least one character must be selected from both p and q.
Step-by-Step Reasoning:
Interleaving Count:
Interleaving means forming r by selecting characters from p and q while maintaining their respective orders.
We use dynamic programming (DP) to count the number of such interleavings.
Define dp[k][i][j] as the number of ways to form the first k characters of r using the first i characters of p and the first j characters of q.
We initialize dp[0][0][0] = 1 since there's exactly one way to form an empty string.
For each character in r, we decide whether to take it from p or q, updating our DP table accordingly.
Subsequences from p and q:
We need to subtract the cases where r is formed entirely from p or entirely from q.
Count subsequences of p that form r and do the same for q using standard DP techniques.
Let ways_p be the number of ways to form r from p, and ways_q be the number of ways to form r from q.
Final Calculation:
The final answer is obtained by subtracting the cases where r is formed solely from p or q from the total interleaving ways:
[
\text{Answer} = \text{Interleaving Ways} - \text{ways_p} - \text{ways_q}
]
Ensure the answer is non-negative by adding (10^9 + 7) before taking the modulo.
Implementation:
Here's the implementation based on the above reasoning:
python
MOD = 10**9 +7
def count_subsequences(S, T):
m, n = len(S), len(T)
dp = [ [0]*(n+1) for _ in range(m+1)]
for i in range(m+1):
dp[i][0] =1
for i in range(1, m+1):
for j in range(1, n+1):
if S[i-1] == T[j-1]:
dp[i][j] = (dp[i-1][j-1] + dp[i-1][j]) % MOD
else:
dp[i][j] = dp[i-1][j]
return dp[m][n]
def count_interleaving(p, q, r):
m, n, l = len(p), len(q), len(r)
# Precompute next_p and next_q
next_p = [dict() for _ in range(m+1)]
last = {}
for i in range(m-1, -1, -1):
last[p[i]] = i
for c in 'abcdefghijklmnopqrstuvwxyz':
next_p[i][c] = last[c] if c in last else m
for c in 'abcdefghijklmnopqrstuvwxyz':
next_p[m][c] = m
next_q = [dict() for _ in range(n+1)]
last = {}
for j in range(n-1, -1, -1):
last[q[j]] = j
for c in 'abcdefghijklmnopqrstuvwxyz':
next_q[j][c] = last[c] if c in last else n
for c in 'abcdefghijklmnopqrstuvwxyz':
next_q[n][c] = n
# Initialize DP
dp = [ [0]*(n+1) for _ in range(m+1)]
dp[0][0] =1
for k in range(l):
c = r[k]
dp_next = [ [0]*(n+1) for _ in range(m+1)]
for i in range(m+1):
for j in range(n+1):
if dp[i][j] ==0:
continue
# Assign to p
x = next_p[i].get(c, m)
if x <m:
dp_next[x+1][j] = (dp_next[x+1][j] + dp[i][j]) % MOD
# Assign to q
y = next_q[j].get(c, n)
if y <n:
dp_next[i][y+1] = (dp_next[i][y+1] + dp[i][j]) % MOD
dp = dp_next
interleaving_ways =0
for i in range(m+1):
j = l -i
if 0 <=j <=n:
interleaving_ways = (interleaving_ways + dp[i][j]) % MOD
return interleaving_ways
def solve():
p = input().strip()
q = input().strip()
r = input().strip()
interleaving = count_interleaving(p, q, r)
ways_p = count_subsequences(p, r)
ways_q = count_subsequences(q, r)
answer = (interleaving - ways_p - ways_q) % MOD
print(answer)
Example:
Using the sample test case:
code
Input:
ab
ba
aba
Output:
2
Explanation:
There are two ways to form "aba":
Take 'a' from p, and 'ba' from q.
Take 'ab' from p, and 'a' from q.
Both maintain the order of characters in p and q and use at least one character from both strings.