# Find all Words from String Present after Given N Words

In this problem, we will find each word of the string which comes after all words of the ‘words’ array.

The first approach to solve the problem is to split the string into words and match elements of the words[] array with the string words. If we find words[] array’s element in the same order inside the string, we print the next word of the string.

Another approach is to create a string of all elements of the words[] array. After that, we can find that string as a substring in the alpha string. If we find it as a substring, we can extract and print the next word.

Problem statement− We have given an alpha string of length N. We have also given a words[] array containing the M words. We need to print each word of the given string, which comes after all occurrences of the words of the ‘words[]’ array in the same order.

Sample examples

Input

alpha = "Javascript is good programming language and C++ is good programming abcd"; words = {"is", "good", "programming"};


Output

language abcd


Explanation – The ‘language’ and ‘abcd’ words come after the ‘is good programming’ in the string.

Input

alpha = "I'm a good programmer, and I'm a good person"; words = {"I'm", "a", "good"};


Output

programmer person


Explanation − The ‘programmer’ and ‘person’ comes after the ‘I’m a good’ substring.

Input

alpha = "Hello Users! How are you?"; words = {"Users", "are", "How"};


Output

“”


Explanation− The occurrences of words[] elements don’t exist in the alpha string. So, it prints the empty string.

## Approach 1

In this approach, we will take all words of the given string in the list. After that, we will traverse the list, and from each index, we will match the next M words with the words[] array’s element, and if all element match, we will print the M + 1 word.

### Algorithm

Step 1 − Initialize the str_words list and temp_str string.

Step 2 − While traversing the string, if the current character is space, push the temp_Str to the str_words list, and reinitialize the temp_str string.

Step 3 − Else, append the current character to the temp_str string.

Step 4 − Also, append the last word to the str_words list.

Step 5− Now, traverse the str_words list. Use nested while loop to match the next M elements of the words array and str_words array starting from the pth index. If any element doesn’t match, break the loop.

Step 6 − If the index of the nested loop is equal to the M, and P + M is less than the str_words list’s size, take the word from the p + 1 index, and push it to the ‘res’ list.

Step 7 − Return the ‘res’ list.

### Example

#include <bits/stdc++.h>
using namespace std;

vector<string> findWords(string alpha, vector<string> &words) {
// To store string words
vector<string> str_words;
string temp_str = "";
// Get each word of the string
for (int p = 0; p < alpha.size(); ++p) {
if (alpha[p] == ' ') {
str_words.push_back(temp_str);
temp_str = "";
} else {
temp_str += alpha[p];
}
}
// Handling the last word
if (temp_str != "")
str_words.push_back(temp_str);
vector<string> res;
int words_len = words.size();
// Traverse the string words and match the given words
for (int p = 0; p <= str_words.size() - words_len; p++)     {
int q = 0;
while (q < words_len) {
// If the word is not matched, break the loop
if (str_words[p + q] != words[q])
break;
q++;
}
// If all words are found, push the next word in the result
if (q == words_len && p + q < str_words.size())
res.push_back(str_words[p + q]);
}
return res;
}
int main() {
string alpha = "Javascript is good programming language and C++ is good programming abcd";
vector<string> words = {"is", "good", "programming"};
vector<string> res = findWords(alpha, words);
cout << "The words according to the problem statement are: ";
for (auto word : res)
cout << word << " ";
return 0;
}


### Output

The words according to the problem statement are: language abcd


Time complexity − O(N*M) to traverse the string and list of words.

Space complexity − O(N) to store string words in the list.

## Approach 2

In this approach, we will append all elements of the words[] array by space seprated. After that, we will find the resultant string as a substring in the original string. We will find the next word in the original string for every occurrence of the resultant string as a substring.

### Algorithm

Step 1 − Initialize the ‘res’ list of strings and the ‘temp_str’ string.

Step 2 − Concatenate all words of the words[] array by separating the space and store into the ‘temp_str’ string.

Step 3 − Start traversing the original string.

Step 4 − If the substring starting from the current index in the original string is the same as a temp_str string, follow the steps below.

Step 5 − Initialize the ‘q’ with the p + temp_str string’s length. After that, use the loop and increment the ‘q’ until we reach the end or the next space character.

Step 6 − Next, take a substring starting from the ‘p + temp_len’ index and of ‘q − p − temp_len’ length, and push to the ‘res’ list.

Step 7 − Return the ‘res’ list.

### Example

#include <bits/stdc++.h>
using namespace std;

vector<string> findWords(string alpha, vector<string> &words) {
vector<string> res;
string temp_str = "";
// Merge all words in a single string
for (int p = 0; p < words.size(); p++) {
temp_str += words[p];
temp_str += " ";
}
int temp_len = temp_str.size();
// Traverse the alpha string and match given words
for (int p = 0; p <= alpha.size() - temp_len; p++) {
// Check if temp_str exists as a substring starting from p index
if (alpha.substr(p, temp_len) == temp_str) {
// Get next word
int q = p + temp_len;
while (q < alpha.size() && alpha[q] != ' ')
q++;
string next_word = alpha.substr(p + temp_len, q - p - temp_len);
res.push_back(next_word);
}
}
return res;
}
int main() {
string alpha = "Javascript is good programming language and C++ is good programming abcd";
vector<string> words = {"is", "good", "programming"};
vector<string> res = findWords(alpha, words);
cout << "The words according to the problem statement are: ";
for (auto word : res)
cout << word << " ";
return 0;
}


### Output

The words according to the problem statement are: language abcd


Time complexity − O(N*M), where O(N) for traversing the original string, and O(M) for finding the substring

Space complexity − O(M) to store the temporary string.

We learned two ways to solve the problem. The first compares the list of words, and another compares the resultant string form by elements of the words[] list with the substring of the original string. Both approaches have the same time and space complexity, so programmers might use any approach for better performance.

Updated on: 17-Jul-2023

37 Views