Count of unique Subsequences of given String with lengths in range [0, N]
Given a string S of length N, the task is to find the number of unique subsequences of the string for each length from 0 to N.
Note: The uppercase letters and lowercase letters are considered different and the result may be large so print it modulo 1000000007.
Examples:
Input: S = “ababd”
Output:
Number of unique subsequences of length 0 is 1
Number of unique subsequences of length 1 is 3
Number of unique subsequences of length 2 is 6
Number of unique subsequences of length 3 is 8
Number of unique subsequences of length 4 is 5
Number of unique subsequences of length 5 is 1Explanation: 0 length subsequences are 1-> {}
1 length subsequences are 3 -> a, b, d
2 length subsequences are 6 -> ab, aa, ad, ba, bd, bb
3 length subsequences are 8 -> aab, aad, abb, abd, bab, aba, bbd, bad
4 length subsequences are 5 -> aabd, abab, abad, babd, abbd
5 length subsequences are 1 -> ababdInput: GeeksForGeeks
Output:
Number of unique subsequences of length 0 is 1
Number of unique subsequences of length 1 is 7
Number of unique subsequences of length 2 is 43
Number of unique subsequences of length 3 is 163
Number of unique subsequences of length 4 is 402
Number of unique subsequences of length 5 is 703
Number of unique subsequences of length 6 is 917
Number of unique subsequences of length 7 is 918
Number of unique subsequences of length 8 is 711
Number of unique subsequences of length 9 is 421
Number of unique subsequences of length 10 is 185
Number of unique subsequences of length 11 is 57
Number of unique subsequences of length 12 is 11
Number of unique subsequences of length 13 is 1
Naive Approach: The basic approach to solve the problem is as follows:
Generate every possible subsequence and store it in a set to get unique results. Then traverse each of the subsequence from that set and count it on the basis of their lengths.
Follow the steps to solve the problem:
- Use recursion to generate each subsequence and store it in a set to get unique occurrences.
- Use a map to store the count of subsequences for each possible length.
- Traverse each subsequence, find the length of the subsequence and update the count of the appropriate group of subsequences.
- Traverse map and print both its keys (length of subsequence) & value (count of subsequence).
Below is the implementation for the above approach:
C++14
// C++ code to implement the above approach. #include <bits/stdc++.h>using namespace std;#define MAX 1000000007typedef long long ll;unordered_set<string> sn; // Function to find subsequencesvoid subsequences(char s[], char op[], ll i, ll j){ if (s[i] == '\0') { op[j] = '\0'; sn.insert(op); return; } else { op[j] = s[i]; subsequences(s, op, i + 1, j + 1); subsequences(s, op, i + 1, j); return; }} map<ll, ll> getCount(){ map<ll, ll> freq; for (auto x : sn) { freq[x.length()] = (freq[x.length()] + 1) % MAX; } return freq;} // Function to print the answervoid printSubsequences(string& S){ ll m = S.length(); char op[m + 1]; subsequences(&S;[0], &op;[0], 0, 0); map<ll, ll> ans = getCount(); for (auto& x : ans) cout << "Number of unique subsequences of length " << x.first << " is " << x.second << endl;} // Driver Codeint main(){ string S = "ababd"; // Function call printSubsequences(S); return 0;} |
Number of unique subsequences of length 0 is 1 Number of unique subsequences of length 1 is 3 Number of unique subsequences of length 2 is 6 Number of unique subsequences of length 3 is 8 Number of unique subsequences of length 4 is 5 Number of unique subsequences of length 5 is 1
Time Complexity: O(2N)
Auxiliary Space: O(N)
Efficient Approach: The idea to solve the problem using dynamic programming is based on the following observations:
Observations:
Consider a character at ith position to be the end character of the subsequence with length j.
Let the total possible ways be denoted as f(i, j).This value depends on the values of f(i-1, j) and f(i-1, j-1), i.e. it is the summation of f(i-1, j) and f(i-1, j-1).
So f(i, j) = f(i-1, j) + f(i-1, j-1)But if the ith character has occurred earlier in an index k, then in the above case, value of f(k-1, j-1) is being considered twice:
- once for f(k, j) and
- next for f(i, j) [as f(k-1, j-1) is a part of f(i-1, j-1) and f(k, j) is part of f(i-1, j)]
and both the time the resulting subsequences are same because the kth and ith character are same.
So, in that case, we need to consider the value only once. Therefore f(i, j) = f(i-1, j) + f(i-1, j-1) – f(k-1, j-1)So there are two cases:
- f(i, j) = f(i-1, j) + f(i-1, j-1) when ith character does not occur earlier
- f(i, j) = f(i-1, j) + f(i-1, j-1) – f(k-1, j-1) when ith character occurs earlier at kth index.
Follow the below steps to solve the problem:
- Create a 2-dimensional array dp[][] where dp[i][j] represents the number of unique subsequences of S until i-th element in string and subsequences are of length j.
- Create an array (say last[]) to store the previous occurrence of a character.
- Use the transition function shown above to calculate the value of dp[i][j].
- Base cases are dp[0][0]=1 and dp[i][j]=0 for every j>i.
Below is the implementation of the above approach:
C++14
// C++ code to implement the approach #include <bits/stdc++.h>using namespace std;typedef long long ll;#define mod 1000000007 // Function to find subsequencesvoid findSubsequences(string& s){ int n = s.length(); ll dp[n + 2][n + 2]; memset(dp, 0, sizeof(dp)); vector<ll> last(256, -1); dp[0][0] = 1; for (int i = 0; i < n + 1; i++) { for (int j = i + 1; j < n + 1; j++) dp[i][j] = 0; } // Loop to implement the dp transition for (int i = 1; i <= n; i++) { for (int j = 0; j < n + 1; j++) { dp[i][j] = (dp[i][j] + dp[i - 1][j]) % mod; if (j >= 1) { dp[i][j] = (dp[i][j] + dp[i - 1][j - 1]) % mod; if (last[s[i - 1]] != -1) { dp[i][j] = (dp[i][j] - dp[last[s[i - 1]]][j - 1]) % mod; } } } last[s[i - 1]] = (i - 1) % mod; } cout << "Number of unique subsequences of length 0 is 1" << endl; for (int i = 1; i <= n; i++) cout << "Number of unique subsequences of length " << i << " is " << dp[n][i] << endl;} // Driver codeint main(){ string S = "ababd"; // Function call findSubsequences(S); return 0;} |
Number of unique subsequences of length 0 is 1 Number of unique subsequences of length 1 is 3 Number of unique subsequences of length 2 is 6 Number of unique subsequences of length 3 is 8 Number of unique subsequences of length 4 is 5 Number of unique subsequences of length 5 is 1
Time Complexity: O(N2)
Auxiliary Space: O(N)


