Sunday, January 29, 2023
HomeSoftware DevelopmentDepend the strings which can be subsequence of the given string

# Depend the strings which can be subsequence of the given string

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
Clarification: “promenade” and “amin” are subsequence of S whereas “proj” just isn’t)

Enter: S = “geeksforgeeks”, arr[] = {“gfg”, “geek”, “geekofgeeks”, “for”}
Output: 3
Clarification:” gfg”, “geek” and “for” are subsequences of S whereas “geekofgeeks” just isn’t.

Naive Method:  The fundamental method to resolve the issue is as follows:

The concept is to examine all strings within the phrases array arr[] that are subsequences of S by recursion.

Beneath 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 aren't matching
return isSubsequence(str1, m, str2, n - 1);
}

// Perform to depend variety of phrases that
// are subsequence of given string S
int countSubsequenceWords(string s, vector<string>& arr)
{

int n = arr.dimension();
int m = s.size();
int res = 0;
for (int i = 0; i < n; i++) {
if (isSubsequence(arr[i], arr[i].dimension(), 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 area

Environment friendly Method: The above strategy will 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 scale of arr.
• Iterate over all of the phrases in arr one after the other.
• Iterate over every character.
• Discover strictly higher index than prevIndex in dict.
• If the strictly higher ingredient just isn’t discovered, it means the present phrase just isn’t a subsequence of the given string, so lower res by 1.
• Else replace prevIndex.
• After iterating over all of the phrases, return ans.

Beneath is the implementation of the above strategy:

## C++

```// C++ code for the above strategy:

#embrace <bits/stdc++.h>
utilizing namespace std;

// Perform 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 dimension of arr
int res = arr.dimension();

for (auto phrase : arr) {

// Index the place final character
// is discovered
int prevIndex = -1;
for (int j = 0; j < phrase.dimension(); j++) {

// Looking for strictly
// higher ingredient than prev
// utilizing binary search
auto x = upper_bound(dict[word[j]].start(),
dict[word[j]].finish(),
prevIndex);

// If strictly higher index
// not discovered, the phrase can't
// 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" };

// Perform 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)

RELATED ARTICLES