Jumbled Strings | Practice | GeeksforGeeks

You are provided an input string S and the string “GEEKS” . Find the number of ways in which the subsequence “GEEKS” can be formed from the string S.
 
Example 1:
Input : S = "GEEKS"
Output: 1
Explanation: 
"GEEKS" occurs in S only once.


Example 2:
Input: S = "AGEEKKSB"
Output: 2
Explanation: Subsequenece "GEEKS" occurs in 
S two times. First one is taking the first 
'K' into consideration and second one is 
taking second 'K'.

 
Your Task:
You don't need to read or print anything. Your task is to complete the function TotalWays() which takes string S as input paramater and returns total ways modulo 109 + 7.
 
Expected Time Complexity : O(N * K) where N is length of string and K is constant.
Expected Space Complexity: O(N * K)
 
Constraints: 
1 <= Length od string <= 10000 
 
0
himanshujain4573 months ago
LCS VARIATION: Count How Many Times String "GEEKS' Occur In String Str As A Subsequence:
class Solution{   public int TotalWays(String str)   {       int n=str.length();       String t="GEEKS";       int mod=(int)1e9+7;       int [][]dp=new int[str.length()+1][6];       for(int i=0;i<=n;i++)       {           for(int j=0;j<=5;j++)           {               if(i==0)               dp[i][j]=0;               if(j==0)               dp[i][j]=1;           }       }        for(int i=1;i<=n;i++)       {           for(int j=1;j<=5;j++)           {              if(str.charAt(i-1)==t.charAt(j-1))              dp[i][j]=(dp[i-1][j-1]%mod+dp[i-1][j]%mod)%mod;              else              dp[i][j]=dp[i-1][j]%mod;           }       }       return dp[n][5];          }}
 
+1
singhanshul28073 months ago
// { Driver Code Starts
//Initial Template for Java
import java.util.*;
import java.lang.*;


//User function Template for Java
class Solution
{
   static int count;
   static HashMap<String,Integer> hm=null;
   public int TotalWays(String s1){
       count=0;
       hm = new HashMap<>();
        return solve(s1,"GEEKS","",s1.length()-1,4);
        
   }
   public int  solve(String s1,String s2,String build,int x1,int x2){
       // System.out.println(build);
       if(build.equals(s2))   {
           return 1;
       }
       if(x1 < 0||x2 < 0)   return 0;
       
       if(hm.containsKey(x1+":"+x2))    return hm.get(x1+":"+x2);
       
       if(s1.charAt(x1) == s2.charAt(x2)){
            int val = (solve(s1,s2,s1.charAt(x1)+build,x1-1,x2-1)+ solve(s1,s2,build,x1-1,x2))%1000000007; 
            hm.put(x1+":"+x2,val);
            return val;
           
       }
       else{
           int val = solve(s1,s2,build,x1-1,x2)%1000000007;
           hm.put(x1+":"+x2,val);
           return val;
       }     
           
         
   }
}
0
abhishekguptaaa3 months ago
SAME CONCEPT OF COUNTING DISTINCT OCCURENCES OF A STRING IN OTHER STRING AS A SUBSEQUENCE:
class Solution{public:int mod=1e9+7;int TotalWays(string str){                           int l=str.length();                  string s="GEEKS";                  int m=s.length();                           int dp[l+1][m+1];                  for(int i=0;i<l+1;i++)         {             for(int j=0;j<m+1;j++)             {                 if(i==0)                 {                     dp[i][j]=0;                 }                 if(j==0)                 {                     dp[i][j]=1;                 }             }         }                  for(int i=1;i<l+1;i++)         {             for(int j=1;j<m+1;j++)             {                 if(str[i-1]==s[j-1])                 {                     dp[i][j]=(dp[i-1][j-1]%mod+dp[i-1][j]%mod)%mod;                 }                 else                 {                     dp[i][j]=(dp[i-1][j])%mod;                 }             }         }                 return dp[l][m]%mod;         }
};
+1
rainamnesh01607 months ago
 int jumble(string s1 , string s2 , int x , int y)
    {
       int t[x+1][y+1];
       
       for(int i = 0 ; i < x+1 ; i++)
       {
           for(int j = 0 ; j < y+1 ; j++)
           {
               if(i==0)
               {
                   if(j==0)
                   t[i][j]= 1;
                   else{
                       t[i][j] = 0;
                   }
               }
               else if(s1[i-1]!=s2[j-1])
               {
                   t[i][j] = t[i-1][j]%mod;
               }
               else{
                   t[i][j] = (t[i-1][j]%mod + t[i-1][j-1]%mod)%mod;
               }
           }
       }
       return t[x][y];
   
    }
	int TotalWays(string str){
	    string str1 = "GEEKS";
	  return jumble(str, str1, str.length(), 5);
	}
0
Prateek Sharma
This comment was deleted.
0
Aman Kaushik9 months ago
Aman Kaushik
Classic Subsequence Pattern.Here is TOP DOWN APPROACHSO HERE IS DP Solution (TOP - DOWN)
int TotalWays(string str)    {        int l=str.length();        string str2 = "GEEKS";        int mod = 1000000007;        int t[l+1][6]; for(int i =0;i<l+1;i++) for(int="" j="0;j&lt;6;j++)" {="" if(i="=0)" t[i][j]="0;" if(j="=0)" t[i][j]="1;" if(i="=0" ||="" j="=0)" continue;="" if(str[i-1]="" !="str2[j-1])" {="" t[i][j]="t[i-1][j];" }="" else{="" t[i][j]="(t[i-1][j-1]" +="" t[i-1][j])%mod;="" }="" }="" return="" t[l][5];="" }="" this="" will="" work="" !="" i="" hope="" this="" help.="">
0
Abhijeet Dwivedi1 year ago
Abhijeet Dwivedi
Classical knapsack problem. Check recursively the last element and see what choices you have in choosing that element as a part of subsequence.
0
Shivanshu Tiwari2 years ago
Shivanshu Tiwari
solution without dp, simple PNC  all test cases pass , 0.01 sechttps://ide.geeksforgeeks.o...
0
Hardik Gupta2 years ago
Hardik Gupta
https://ide.geeksforgeeks.o...Execution time: 0.01Simple solutionLittle bit of PnC.
0
Ayush Kumar2 years ago
Ayush Kumar
fn(p,q)  :             if s1[p]==s2[q] : fn(p-1,q)+fn(p-1,q-1)             otherwise        : fn(p-1,q) p is index in the original string and q is for string "GEEKS"So, fn(n,5) is made.
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.