Given three strings p, q, r. Count the possible number of ways to create string r using…
- User
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 for the above question i written code def func(p, q, r, a, b, c, pl, ql, rl): if c == rl: return 1 if a > 0 and b > 0 else 0 if c > pl + ql: return 0 count = 0 if a < pl and p[a] == r[c]: count += func(p, q, r, a + 1, b, c + 1, pl, ql, rl) % 1000000007 if b < ql and q[b] == r[c]: count += func(p, q, r, a, b + 1, c + 1, pl, ql, rl) % 1000000007 if a < pl and b < ql and p[a] == q[b] == r[c]: count += func(p, q, r, a + 1, b + 1, c + 1, pl, ql, rl) % 1000000007
return countdef solution(p, q, r): pl = len(p) ql = len(q) rl = len(r) total_ways = func(p, q, r, 0, 0, 0, pl, ql, rl) return total_ways. it is failing some test cases make it correct
- Assistant