Given two string ‘S1’ and ‘S2’, the task is to return the most frequent (which is used the maximum number of times) word from ‘S1’ that is not present in ‘S2’. If more than one word is possible then print lexicographically smallest among them.
Input: S1 = “geeks for geeks is best place to learn”, S2 = “bad place”
“geeks” is the most frequent word in S1 and is also not present in S2.
The frequency of “geeks” is 2
Input: S1 = “the quick brown fox jumps over the lazy dog”, S2 = “the brown fox jumps”
All the words have frequency 1.
The lexicographically smallest word is “dog”
Approach: The thought process must begin with the creation of a map to store key-value pair( string, int). Following this begins the extraction of the words from the first string while updating the map and the count. For every word from the second array that is present in the first array, reset the count. Finally, traverse the map and find the word with the highest frequency and get the lexicographically smallest one.
- Iterate through string S2 and create a map and insert all of the words in it to the map.
- Iterate through string S1 and check whether the word is not present in the map created in the previous step or not.
- If the word satisfies the condition then update the answer if the frequency of the same word is maximum till now.
- If the frequency of the word is equal to the previously chosen word then update the answer according to lexicographically smallest of the two strings.
Below is the implementation of above approach:
- Time Complexity: O(n), where n is the length of the string.
A single traversal of the string is needed.
- Space Complexity: O(n).
There can be at most n words in a string. The map requires O(n) space to store the strings.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Remove characters from the first string which are present in the second string
- Program to find second most frequent character
- Most frequent word in an array of strings
- Count of substrings having the most frequent character in the string as first character
- Find the character in first string that is present at minimum index in second string
- Second most repeated word in a sequence
- Second most repeated word in a sequence in Python
- Find the most frequent digit without using array/string
- Kth most frequent Character in a given String
- Most frequent element in Array after replacing given index by K for Q queries
- Count subsequences in first string which are anagrams of the second string
- Count of strings in the first array which are smaller than every string in the second array
- Count number of rotated strings which have more number of vowels in the first half than second half
- Check if a word is present in a sentence
- Next word that does not contain a palindrome and has characters from first k
- Longest Common Prefix using Word by Word Matching
- C program to Replace a word in a text by another given word
- C program to find and replace a word in a File by another given word
- Find the word from a given sentence having given word as prefix
- Find the longest sub-string which is prefix, suffix and also present inside the string
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.