String Subsequence | Practice | GeeksforGeeks

Given two strings S1 and S2, find the number of times the second string occurs in the first string, whether continuous or discontinuous.

Example 1:
Input: 
S1 = geeksforgeeks
S2 = gks
Output: 4
Explaination: For the first 'g' there 
are 3 ways and for the second 'g' there 
is one way. Total 4 ways.

Your Task:
You don't need to read input or print anything. Your task is to complete the function countWays() which takes the string S1 and S2 as input parameters and returns the number of ways.

Expected Time Complexity: O(n*m)        [n and m are length of the strings]
Expected Auxiliary Space: O(n*m)

Constraints:
1 ≤ n, m ≤ 500
+1
abhishekvicky123453 months ago
/*JAVA Solution*/
 
class Solution {   int countWays(String S1, String S2) {        int[][] ary=new int[S2.length()+1][S1.length()+1];       for(int i=0;i<=S2.length();i++)       {           for(int j=0;j<=S1.length();j++)           {               if(i==0)             ary[i][j]=1;             else if(j==0)             ary[i][j]=0;             else if(S2.charAt(i-1)==S1.charAt(j-1))             ary[i][j]=ary[i][j-1]+ary[i-1][j-1];             else if(S2.charAt(i-1)!=S1.charAt(j-1))             ary[i][j]=ary[i][j-1];           }       }       return ary[S2.length()][S1.length()];   }}
+1
archigoyal3 months ago
/* 1D ARRAY, DP SOLUTION */ int countWays(String s1, String s2) {        // code here       // int [] ofs2=new int[s2.length()];       int n=s1.length();       int m=s2.length();       int []dp=new int[m];       for(int i=0;i<m;i++) dp[i]=0;       int old=1,next=1;       for(int i=0;i<n;i++){           for(int j=0;j<m;j++){               old=dp[j];               if(s1.charAt(i)==s2.charAt(j))               {                  if(j==0)next=1;                   dp[j]=next+dp[j];               }               next=old;           }       }
       return dp[m-1];   }
+1
aaishuagarwal3 months ago
class Solution{public:int count=0;void helper(string s1,string s2,int m,int n){   if(m==0)   {       return;   } if(s1[m-1]==s2[n-1])   {       if(n==1)       count++;       helper(s1,s2,m-1,n-1);     }       helper(s1,s2,m-1,n);}   int countWays(string s1, string s2){       // code here       int m=s1.length();       int n=s2.length();   helper(s1,s2,m,n);     return count;         }};
0
6288264 months ago
Decent.
 
void print(string s1, string s2, vector<vector<int>> &dp)
{
	cout << "  ";
	
	for (int j = 0; j < s2.size(); ++j)
		cout << s2[j] << " ";
	cout << endl;
	
	for (int i = 0; i < s1.size(); ++i)
	{
		cout << s1[i] << " ";
		for (int j = 0; j < s2.size(); ++j)
			cout << dp[i + 1][j + 1] << " ";
		cout << endl;
	}
}

int countWays(string S1, string S2){
	int m = S1.size();
	int n = S2.size();
	vector<vector<int>> dp(m + 1, vector<int>(n + 1));
	
	for (int i = 0; i < m; ++i)
		dp[i][0] = 1;
	
	for (int i = 0; i < m; ++i)
		for (int j = 0; j < n; ++j)
			dp[i + 1][j + 1] = dp[i][j + 1] + (S1[i] == S2[j] ? dp[i][j] : 0);
		
	//print(S1, S2, dp);
		
	return dp[m][n];
}
+1
imranwahid4 months ago
Easy C++ solution using recursion
0
jhapratik20004 months ago
why is the ans=1 when the string s2 is empty and s1 is not empty ..like how empty string can occur in s1 ?
0
sparshrastogi14 months ago
#PYTHON SOLUTION WITH TOPDOWN APPROCH
def countWays(self, S1, S2):       # code here        dp = []       m = len(S1)       n = len(S2)       for i in range(m+1):           a = [0]*(n+1)           dp.append(a)           dp[i][0] = 1       for i in range(1,m+1):           for j in range(1,n+1):               if S1[i-1] == S2[j-1]:                   dp[i][j] = dp[i-1][j]+dp[i-1][j-1]               else:                   dp[i][j] = dp[i-1][j]       return dp[m][n]
0
anujch6 months ago
int solve(string s1,string s2,int i,int j){      int cnt=0;   if(j==s2.length()-1)   {      for(int k=i;k<s1.length();k++)      {          if(s1[k]==s2[j])          cnt++;                }     return cnt;   }          for(int l=i;l<s1.length();l++)       {           if(s1[l]==s2[j])           {               cnt+=solve(s1,s2,l+1,j+1);           }       }   return cnt;}   int countWays(string S1, string S2){      return solve(S1,S2,0,0);   }
0
The Nerd8 months ago
The Nerd
https://www.geeksforgeeks.o...Hope this helps.
0
Ankit Podder10 months ago
Ankit Podder
class Solution{public:    int recFuc(string s1 , string s2 , int a , int b ,    vector<vector<int>> &dp){        if(dp[a][b] != -1) return dp[a][b];        else if(a==0) return 0;        if(s1[a-1] == s2[b-1])            dp[a][b] =  recFuc(s1 , s2 , a-1 , b-1 , dp) + recFuc(s1 , s2 , a-1 , b , dp);        else            dp[a][b] = recFuc(s1 , s2 , a-1 , b , dp);        return dp[a][b];    }    int countWays(string S1, string S2){        vector<vector<int>> dp(S1.length()+1 , vector<int> (S2.length()+1 , -1));        dp[0][0] = 1;        return recFuc(S1 , S2 , S1.length() , S2.length() , dp);    }};
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.