# Count distinct occurrences as a subsequence

Given a two strings S and T, find count of distinct occurrences of T in S as a subsequence.

Examples:

Input : S = banana, T = ban Output : 3 T appears in S as below three subsequences. [ban], [ba n], [b an] Input : S = geeksforgeeks, T = ge Output : 6 T appears in S as below three subsequences. [ge], [ ge], [g e], [g e] [g e] and [ g e]

This problem can be recursively defined as below.

// Returns count of subsequences of S that match T // m is length of T and n is length of S subsequenceCount(S, T, n, m) // An empty string is subsequence of all. 1) If length of T is 0, return 1. // Else no string can be a sequence of empty S. 2) Else if S is empty, return 0. 3) Else if last characters of S and T don't match, remove last character of S and recur for remaining return subsequenceCount(S, T, n-1, m) 4) Else (Last characters match), the result issumof two counts. // Remove last character of S and recur. a) subsequenceCount(S, T, n-1, m) + // Remove last characters of S and T, and recur. b) subsequenceCount(S, T, n-1, m-1)

Since there are overlapping subproblems in above recurrence result, we can apply dynamic programming approach to solve above problem. We create a 2D array mat[m+1][n+1] where m is length of string T and n is length of string S. mat[i][j] denotes the number of distinct subsequence of substring S(1..i) and substring T(1..j) so mat[m][n] contains our solution.

## C++

`/* C/C++ program to count number of times S appears ` ` ` `as a subsequence in T */` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `int` `findSubsequenceCount(string S, string T) ` `{ ` ` ` `int` `m = T.length(), n = S.length(); ` ` ` ` ` `// T can't appear as a subsequence in S ` ` ` `if` `(m > n) ` ` ` `return` `0; ` ` ` ` ` `// mat[i][j] stores the count of occurrences of ` ` ` `// T(1..i) in S(1..j). ` ` ` `int` `mat[m + 1][n + 1]; ` ` ` ` ` `// Initializing first column with all 0s. An empty ` ` ` `// string can't have another string as suhsequence ` ` ` `for` `(` `int` `i = 1; i <= m; i++) ` ` ` `mat[i][0] = 0; ` ` ` ` ` `// Initializing first row with all 1s. An empty ` ` ` `// string is subsequence of all. ` ` ` `for` `(` `int` `j = 0; j <= n; j++) ` ` ` `mat[0][j] = 1; ` ` ` ` ` `// Fill mat[][] in bottom up manner ` ` ` `for` `(` `int` `i = 1; i <= m; i++) { ` ` ` `for` `(` `int` `j = 1; j <= n; j++) { ` ` ` `// If last characters don't match, then value ` ` ` `// is same as the value without last character ` ` ` `// in S. ` ` ` `if` `(T[i - 1] != S[j - 1]) ` ` ` `mat[i][j] = mat[i][j - 1]; ` ` ` ` ` `// Else value is obtained considering two cases. ` ` ` `// a) All substrings without last character in S ` ` ` `// b) All substrings without last characters in ` ` ` `// both. ` ` ` `else` ` ` `mat[i][j] = mat[i][j - 1] + mat[i - 1][j - 1]; ` ` ` `} ` ` ` `} ` ` ` ` ` `/* uncomment this to print matrix mat ` ` ` `for (int i = 1; i <= m; i++, cout << endl) ` ` ` `for (int j = 1; j <= n; j++) ` ` ` `cout << mat[i][j] << " "; */` ` ` `return` `mat[m][n]; ` `} ` ` ` `// Driver code to check above method ` `int` `main() ` `{ ` ` ` `string T = ` `"ge"` `; ` ` ` `string S = ` `"geeksforgeeks"` `; ` ` ` `cout << findSubsequenceCount(S, T) << endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to count number of times ` `// S appears as a subsequence in T ` `import` `java.io.*; ` ` ` `class` `GFG { ` ` ` `static` `int` `findSubsequenceCount(String S, String T) ` ` ` `{ ` ` ` `int` `m = T.length(); ` ` ` `int` `n = S.length(); ` ` ` ` ` `// T can't appear as a subsequence in S ` ` ` `if` `(m > n) ` ` ` `return` `0` `; ` ` ` ` ` `// mat[i][j] stores the count of ` ` ` `// occurrences of T(1..i) in S(1..j). ` ` ` `int` `mat[][] = ` `new` `int` `[m + ` `1` `][n + ` `1` `]; ` ` ` ` ` `// Initializing first column with ` ` ` `// all 0s. An emptystring can't have ` ` ` `// another string as suhsequence ` ` ` `for` `(` `int` `i = ` `1` `; i <= m; i++) ` ` ` `mat[i][` `0` `] = ` `0` `; ` ` ` ` ` `// Initializing first row with all 1s. ` ` ` `// An empty string is subsequence of all. ` ` ` `for` `(` `int` `j = ` `0` `; j <= n; j++) ` ` ` `mat[` `0` `][j] = ` `1` `; ` ` ` ` ` `// Fill mat[][] in bottom up manner ` ` ` `for` `(` `int` `i = ` `1` `; i <= m; i++) { ` ` ` `for` `(` `int` `j = ` `1` `; j <= n; j++) { ` ` ` `// If last characters don't match, ` ` ` `// then value is same as the value ` ` ` `// without last character in S. ` ` ` `if` `(T.charAt(i - ` `1` `) != S.charAt(j - ` `1` `)) ` ` ` `mat[i][j] = mat[i][j - ` `1` `]; ` ` ` ` ` `// Else value is obtained considering two cases. ` ` ` `// a) All substrings without last character in S ` ` ` `// b) All substrings without last characters in ` ` ` `// both. ` ` ` `else` ` ` `mat[i][j] = mat[i][j - ` `1` `] + mat[i - ` `1` `][j - ` `1` `]; ` ` ` `} ` ` ` `} ` ` ` ` ` `/* uncomment this to print matrix mat ` ` ` `for (int i = 1; i <= m; i++, cout << endl) ` ` ` `for (int j = 1; j <= n; j++) ` ` ` `System.out.println ( mat[i][j] +" "); */` ` ` `return` `mat[m][n]; ` ` ` `} ` ` ` ` ` `// Driver code to check above method ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `String T = ` `"ge"` `; ` ` ` `String S = ` `"geeksforgeeks"` `; ` ` ` `System.out.println(findSubsequenceCount(S, T)); ` ` ` `} ` `} ` `// This code is contributed by vt_m ` |

*chevron_right*

*filter_none*

## C#

`// C# program to count number of times ` `// S appears as a subsequence in T ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `static` `int` `findSubsequenceCount(` `string` `S, ` `string` `T) ` ` ` `{ ` ` ` `int` `m = T.Length; ` ` ` `int` `n = S.Length; ` ` ` ` ` `// T can't appear as a subsequence in S ` ` ` `if` `(m > n) ` ` ` `return` `0; ` ` ` ` ` `// mat[i][j] stores the count of ` ` ` `// occurrences of T(1..i) in S(1..j). ` ` ` `int` `[, ] mat = ` `new` `int` `[m + 1, n + 1]; ` ` ` ` ` `// Initializing first column with ` ` ` `// all 0s. An emptystring can't have ` ` ` `// another string as suhsequence ` ` ` `for` `(` `int` `i = 1; i <= m; i++) ` ` ` `mat[i, 0] = 0; ` ` ` ` ` `// Initializing first row with all 1s. ` ` ` `// An empty string is subsequence of all. ` ` ` `for` `(` `int` `j = 0; j <= n; j++) ` ` ` `mat[0, j] = 1; ` ` ` ` ` `// Fill mat[][] in bottom up manner ` ` ` `for` `(` `int` `i = 1; i <= m; i++) { ` ` ` ` ` `for` `(` `int` `j = 1; j <= n; j++) { ` ` ` ` ` `// If last characters don't match, ` ` ` `// then value is same as the value ` ` ` `// without last character in S. ` ` ` `if` `(T[i - 1] != S[j - 1]) ` ` ` `mat[i, j] = mat[i, j - 1]; ` ` ` ` ` `// Else value is obtained considering two cases. ` ` ` `// a) All substrings without last character in S ` ` ` `// b) All substrings without last characters in ` ` ` `// both. ` ` ` `else` ` ` `mat[i, j] = mat[i, j - 1] + mat[i - 1, j - 1]; ` ` ` `} ` ` ` `} ` ` ` ` ` `/* uncomment this to print matrix mat ` ` ` `for (int i = 1; i <= m; i++, cout << endl) ` ` ` `for (int j = 1; j <= n; j++) ` ` ` `System.out.println ( mat[i][j] +" "); */` ` ` `return` `mat[m, n]; ` ` ` `} ` ` ` ` ` `// Driver code to check above method ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `string` `T = ` `"ge"` `; ` ` ` `string` `S = ` `"geeksforgeeks"` `; ` ` ` `Console.WriteLine(findSubsequenceCount(S, T)); ` ` ` `} ` `} ` ` ` `// This code is contributed by vt_m ` |

*chevron_right*

*filter_none*

Output:

6

Time Complexity : O(m*n)

Auxiliary Space : O(m*n)

Since mat[i][j] accesses elements of current row and previous row only, we can optimize auxiliary space just by using two rows only reducing space from m*n to 2*n.

This article is contributed by **Utkarsh Trivedi**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

## Recommended Posts:

- Find all distinct subset (or subsequence) sums of an array | Set-2
- Find all distinct subset (or subsequence) sums of an array
- Count Distinct Subsequences
- Count distinct elements in an array
- Count of subsequences having maximum distinct elements
- Absolute distinct count in a sorted array
- Count all distinct pairs with difference equal to k
- Count All Palindromic Subsequence in a given String
- Count subsequence of length three in a given string
- Count common subsequence in two strings
- Maximum length subsequence such that adjacent elements in the subsequence have a common factor
- Printing frequency of each character just after its consecutive occurrences
- Maximum even sum subsequence
- Subsequence with maximum odd sum
- Longest Zig-Zag Subsequence