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.
Input: S1 = “the quick brown fox jumps over the lazy dog”, S2 = “the brown fox jumps”
- 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:
# Python3 implementation of above approach
from collections import defaultdict
# Function to return frequent
# word from S1 that isn’t
# present in S2
def smallestFreq(S1, S2):
banned = defaultdict(lambda:0)
i = 0
# create map of banned words
while i < len(S2): s = "" while i < len(S2) and S2[i] != ' ': s += S2[i] i += 1 i += 1 banned[s] += 1 result = defaultdict(lambda:0) ans = "" freq = 0 i = 0 # find smallest and most frequent word while i < len(S1): s = "" while i < len(S1) and S1[i] != ' ': s += S1[i] i += 1 i += 1 # check if word is not banned if banned[s] == 0: result[s] += 1 if (result[s] > freq or
(result[s] == freq and s < ans)): ans = s freq = result[s] # return answer return ans # Driver Code if __name__ == "__main__": S1 = "geeks for geeks is best place to learn" S2 = "bad place" print(smallestFreq(S1, S2)) # This code is contributed # by Rituraj Jain [tabbyending]
- Find the longest sub-string which is prefix, suffix and also present inside the string
- Find the character in first string that is present at minimum index in second string
- Remove characters from the first string which are present in the second string
- String containing first letter of every word in a given string with spaces
- Find the most frequent digit without using array/string
- Most frequent word in an array of strings
- Count words present in a string
- Calculate sum of all numbers present in a string
- Number of substrings of one string present in other
- Python | Check if a Substring is Present in a Given String
- Check whether the Average Character of the String is present or not
- Find repeated character present first in a string
- Length Of Last Word in a String
- Program to print characters present at prime indexes in a given string
- Find the first repeated word in a 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 firstname.lastname@example.org. 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.
Improved By : rituraj_jain