Ninja And The Game Of Words
Posted: 27 Feb, 2021
Difficulty: Moderate
Ninja and his friend playing a game in which his friend gave him a string ‘STR’ that can contain spaces and a List/Array ‘WORDS’ which is of type string containing ‘N’ strings of words. Ninja’s task is to count the occurrences of all the words in ‘STR’.
For Example:
‘STR’ = “i am a Ninja”, ‘N’ = 3 and ‘WORDS[]’ = [“Ninja”,”a”,”am”]. Then the output should be [1,3,1]. Because the occurrence of “Ninja” in ‘STR’ is 1 and the occurrence of “a” in ‘STR’ is 3.Similarly occurrence of “am” is 1.
Note:
The output should be in the same order as given in ‘WORDS’.
Can you help Ninja to generate all valid strings from ‘STR’ by minimum removals?
Input Format:
The first line of input contains an integer 'T' which denotes the number of test cases.
The first line of each test case contains an integer ‘N’.
The second line contains a string ‘STR’.
The next ‘N’ lines contain a string representing words.
Output Format :
For each test case, return the frequency of all the words given in ‘WORDS’ Array/List
Print the output for each test case in a separate line.
Note:
You don't need to print anything; it has already been taken care of. Just implement the given function.
Constraints:
1 <= ‘T’ <= 100
1 <= |STR| <= 4000
1<= N <= 4000
1<= |WORDS[i]| <= 4000
Where |'STR'| denotes the length of the given string and ‘|WORDS[i]|’ denotes the length of the string word.
Time limit: 1 sec
Approach 1
The idea behind this approach is to one by one check all the words in ‘WORDS’ if they are present in ‘STR’ or NOT. If present then increases the count of that word.
Here is the complete algorithm:
- Make an array/list ‘ANSWER’ to store the count of occurrence of each word. Iterate the ‘WORDS’ and for each word ‘WORD[i]’ at index ‘i’ in the ‘WORDS’ do the following:
- Inetialize a variabe ‘COUNT’ = 0.
- Iterate the ‘STR’ and for each index ‘j’ do the following:
- If the substring of length from ‘j’ to ‘j + lengthof(WORD[i])’ is equal to the ‘WORD[i]’ then increase the ‘count’ by ‘1’.
- Insert the value of ‘COUNT’ in the ‘ANSWER’.
- Finally, return ‘ANSWER’.
Approach 2
The idea behind this approach is to store all the words in the ‘STR’ in a map with its frequency and then traverse ‘WORDS’ and for each word in ‘WORDS’ get the frequency from the map.
Here is the complete algorithm:
- Make a hash map ‘MP’ of type ‘MP<STRING, INT>’ and an array/list ‘ANSWER’ to store the frequency of all the words in the ‘WORDS’.
- Iterate the ‘STR’ and extract the words one by one and for each word insert this word in the ‘MP’. There are two following cases:
- If the word is already present in the ‘MP’ then simply increase its count.
- Else insert the word with frequency ‘1’.
- Iterate the ‘WORDS’ and for each ‘WORD[i]’ in the ‘WORDS’ does the following:
- Get the frequency of ‘WORDS[i]’ and insert it in the answer.
Finally, return the ‘ANSWER’.
SIMILAR PROBLEMS
Count Couples
Posted: 10 Dec, 2021
Difficulty: Easy
Count Palindromic Pairs
Posted: 11 Dec, 2021
Difficulty: Moderate
Second Most Repeated Word
Posted: 11 Dec, 2021
Difficulty: Easy
Railway Management
Posted: 15 Dec, 2021
Difficulty: Moderate
Treasure and Jewels
Posted: 8 Jan, 2022
Difficulty: Easy