Get the intuition behind solving this problem!

Revision en1, by Varun_Shah, 2018-09-17 18:51:20

I found the following problem very interesting on a recent Hackerrank contest, I don't get the intuition for what the editorialist states.It would indeed be very helpful if someone helps me understand it.

What I got is as follows:

for number of ways of reaching a solution from either string a or string b, the recurrence may go as (dp[x...y] means dp[x] + dp[x+1] ... + dp[y])

dp[i][j][k][0] = summation of dp[i-1][j-1][k][0...2];

dp[i][j][k][1] = (if a[i] equals c[k] then summation of dp[i-1][j][k-1][0...2] else 0) + dp[i-1][j][k][0...2];

dp[i][j][k][2] = (if b[j] equals c[k] then summation of dp[i][j-1][k-1][0...2] else 0) + dp[i][j-1][k][0...2];

Here, dp[i][j][k][x] means number of ways of forming c[0...k] from a[0...i] or from b[0...j] or both.

where x = 0 if we neglect last character of both a and b = 1 if we consider last character of a and not b = 2 if we consider last character of b and not a

Please correct me if I am wrong.Thanks in advance for the help.

#### History

Revisions

Rev. Lang. By When Δ Comment
en3 Varun_Shah 2018-09-17 19:16:00 14 Tiny change: ' not b\n\n x = 2 if w' -> ' not b\n\nx = 2 if w'
en2 Varun_Shah 2018-09-17 18:52:36 108
en1 Varun_Shah 2018-09-17 18:51:20 1182 Initial revision (published)