Jumbled Strings
You are provided an input string S and the string "includehelp". You need to figure out all possible subsequences "includehelp" in the string S? Find out the number of ways in which the subsequence "includehelp" can be formed from the string S.
Input:
Input is the string s
Output:
Corresponding to each test case, print in a new line, a number denoting the number of ways in which we can form the subsequence "includehelp". Since output can be very large find the answer modulo 1000000007.
Constraints:
Input String contains only lowercase English Letters and string length is 5000 at maximum.
Example:
Input:
includehelp
Output:
1
Explanation:
There is only one instances of "includehelp"
in the above input string.
Input:
iincludehelp
Output:
2
Explanation:
There is two instances of "includehelp"
in the above input string.

Let the input string be, s and t="includehelp" This problem can be solved by recursion. So we have, string s: the input string string t: the second string ("includehelp") starts : start point of string s srartt : start point of string t, ("includehelp") m: length of input string 11: length of string t,"includehelp" MOD: 1000000009Now, how can we generate a recursive relation?
Say starts=i where 0<=i<m & startt=j where 0<=j<11
Say,
Now we have two options,
Now we have only one option which is check for starts+1, startt as we need to look for different occurrence only.
Function: jumbleString(string s,string t,int starts,int startt,int m) // enter substring is matched if startt==11 return 1; // enter string has been searched with out match if starts==m return 0; if(s[starts]!=t[startt]) //only one option as we discussed return jumbleString(s,t,starts+1,startt,m)%MOD; else // both the options as we discussed return (jumbleString(s,t,starts+1,startt+1,m)%MOD + jumbleString(s,t,starts+1,startt,m)%MOD)%MODThe above recursion will generate many overlapping subproblems and hence we need to use dynamic programming.
Let's convert the recursion to DP.
for i=0 to 11 dp[0][i]=0;for i=0 to m dp[i][0]=1;for i=1 to m for j=1 to 11 if s[i-1]==t[j-1] dp[i][j]=(dp[i-1][j]+dp[i-1][j-1])%MOD else dp[i][j]=dp[i-1][j] end for end forThe above DP technique is known as the tabulation process. We can introduce memorization as well, known as the top-down approach. Where we store every computed subproblem and while computing first we look up our DP table whether sub-problem is already solved or not. Check the below top-down implementation for the above problem.
C++ Implementation:
Output:
need an explanation for this answer? contact us directly to get an explanation for this answer