Given a string S and an array arr[] of phrases, the duty is to return the quantity of phrases from the array which is a subsequence of S.
Examples:
Enter: S = “programming”, arr[] = {“promenade”, “amin”, “proj”}
Output: 2
Rationalization: “promenade” and “amin” are subsequence of S whereas “proj” will not be)Enter: S = “geeksforgeeks”, arr[] = {“gfg”, “geek”, “geekofgeeks”, “for”}
Output: 3
Rationalization:” gfg”, “geek” and “for” are subsequences of S whereas “geekofgeeks” will not be.
Naive Method: The essential approach to remedy the issue is as follows:
The thought is to verify all strings within the phrases array arr[] that are subsequences of S by recursion.
Under is the implementation of the above strategy:
C++
// C++ code for the above strategy: #embrace <bits/stdc++.h> utilizing namespace std; bool isSubsequence(string& str1, int m, string& str2, int n) { if (m == 0) return true; if (n == 0) return false; // If final characters of two strings // are matching if (str1[m - 1] == str2[n - 1]) return isSubsequence(str1, m - 1, str2, n - 1); // If final characters will not be matching return isSubsequence(str1, m, str2, n - 1); } // Operate to depend variety of phrases that // are subsequence of given string S int countSubsequenceWords(string s, vector<string>& arr) { int n = arr.measurement(); int m = s.size(); int res = 0; for (int i = 0; i < n; i++) { if (isSubsequence(arr[i], arr[i].measurement(), s, m)) { res++; } } return res; } // Driver Code int predominant() { string S = "geeksforgeeks"; vector<string> arr = { "geek", "for", "geekofgeeks", "gfg" }; // Funtion name cout << countSubsequenceWords(S, arr) << "n"; return 0; }
Time Complexity: O(m*n)
Auxiliary House: O(m) for recursion stack house
Environment friendly Method: The above strategy may be optimized based mostly on the next thought:
- Map the index of characters of the given string to the respective characters array.
- Initialize the ans with the dimensions of arr.
- Iterate over all of the phrases in arr one after the other.
- Iterate over every character.
- Discover strictly better index than prevIndex in dict.
- If the strictly better component will not be discovered, it means the present phrase will not be a subsequence of the given string, so lower res by 1.
- Else replace prevIndex.
- After iterating over all of the phrases, return ans.
Under is the implementation of the above strategy:
C++
// C++ code for the above strategy: #embrace <bits/stdc++.h> utilizing namespace std; // Operate to depend variety of phrases that // are subsequence of given string S int countSubsequenceWords(string s, vector<string>& arr) { unordered_map<char, vector<int> > dict; // Mapping index of characters of given // string to respective characters for (int i = 0; i < s.size(); i++) { dict[s[i]].push_back(i); } // Initializing res with measurement of arr int res = arr.measurement(); for (auto phrase : arr) { // Index the place final character // is discovered int prevIndex = -1; for (int j = 0; j < phrase.measurement(); j++) { // Trying to find strictly // better component than prev // utilizing binary search auto x = upper_bound(dict[word[j]].start(), dict[word[j]].finish(), prevIndex); // If strictly better index // not discovered, the phrase can not // be subsequence of string s if (x == dict[word[j]].finish()) { res--; break; } // Else, replace the prevIndex else { prevIndex = *x; } } } return res; } // Driver Code int predominant() { string S = "geeksforgeeks"; vector<string> arr = { "geek", "for", "geekofgeeks", "gfg" }; // Operate name cout << countSubsequenceWords(S, arr) << "n"; return 0; }
Time Complexity: O( m * s * log(n) ), the place m is the size of the given string, s is the max size of the phrase of arr and n is the size of arr
Auxiliary House: O(n)