- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Count of N length Strings having S as a Subsequence
We are given a string of length S and given an another number n which represents the length of the strings which might contains S as the subsequence. We have to find the number of unique strings of length N which contains S as the subsequence, where a subsequence is the set of characters from the given string which may be all the characters or not and they not need to be continuous.
Sample Examples
Input
string str = "xyz" int n = 3
Output
1
Explanation
There is only one string of length 3 that contains the given string as its subsequence.
Input
string str = "aback" int n = 4
Output
0
Explanation
No string of length 4 can store the subsequence of size 5 or the size of given string is more than the given string so there is no solution.
Input
string str = "aba" int n = 5
Output
6376
Approach
We have seen the example above to get an idea about the problem. In this approach we are going to use the concepts of the permutations and the combinations.
We will create a function which will take the current string and the given number N as the input and will return an integer representing the required number of strings.
In the function we will create an array which will store the factorial result for each number 0 to N both inclusive.
We will use the for loop to traverse over the range from the length of the given string and to the given number N.
To get the nCr value for the number of combinations we need to define another function that will take the n, r and factorial as the input and will return required value.
We will define another function getPower that will return the power of given number to the given value but the catch is it will return the value with the mod.
We will call to nCr function and from there we will call to the power function and get the required methods which we can see in the code.
Example
#include <bits/stdc++.h> using namespace std; int mod = 1e9 + 7; // mod value to take mod with // function to get the power as the function of mod long long getPower(long long num1, long long num2){ long long ans = 1; num1 = num1 % mod; if(num1 == 0){ return 0; } while(num2 > 0){ if(num2 & 1){ ans = (ans * num1) % mod; } num2 /= 2; num1 = (num1 * num1) % mod; } return ans; } // get the value of the nCr long long nCr(int n, int r, long long fact[]){ // base case if(r > n){ return 0; } long long res = fact[n]; res *= getPower(fact[r], mod - 2); res = res % mod; res *= getPower(fact[n - r], mod - 2); res = res % mod; return res; } // function to get the required number of int numberOfStrings(string str, int n){ int len = str.length(); // get the lenght of the given string // if the n is less than than given stirng size // then there will be no string present of given length if(len > n){ return 0; } // array to store the value of the factorials long long fact[n + 1]; fact[0] = 1; // initializing 0th index value // calculating the result for the other indexes for(int i = 1; i <= n; i++){ fact[i] = fact[i - 1] * i; fact[i] %= mod; } // variable to store the result long long res = 0; // iterating over the all the possible positions for (int i = len; i <= n; i++){ long long temp = nCr(i-1, len-1, fact); temp *= getPower(26, n - i); temp %= mod; temp *= getPower(25, i - len); temp %= mod; res += temp; res %= mod; } return res; } int main(){ int n = 5; string str = "aba"; cout << "The number of strings of n length having str as a Subsequence are: "<< numberOfStrings(str, n)<<endl; return 0; }
Output
The number of strings of n length having str as a Subsequence are: 6376
Time and Space Complexity
The time complexity of the above code is O(N*log(N)), where N is the given limit of character of sequence.
The space complexity of the above code is O(N), as we are using an array to store the factorial upto length N.
Conclusion
In this tutorial, we have implemented a program to find the count of the number of strings of given length N that contains the given string S as the subsequence and as the count of string can be high so we need to take mod with the 1e9 + 7. We have implemented a approach using the factorial and combinations methods with the help of the power with the mod.