Most Frequent Word

You are given a paragraph that may have letters both in lowercase and uppercase, spaces, and punctuation. You have also given a list of banned words. Now your task is to find the most frequent word which is not in the list of banned words. There will always be a solution, and the solution will be unique.

While comparing words, you can ignore whether the letter is lowercase or uppercase. For example, ‘AsK’ and ‘aSK’ are the same. The words will always contain only alphabets or we can say words will be separated by spaces or punctuation. The answer will be in uppercase letters.

The words in the banned list will always be in upper letters and free from punctuation and spaces.

For example :

Paragraph = ‘It's a square SqUare. It's a FLAT flat.’ 
Banned =[FLAT, IT, S]. 
So we can see these words [IT, S, SQUARE, FLAT ]  are most frequent.
Now we will look at to banned list and we can see 3 of the words are banned.
So we have a unique answer SQUARE which has a frequency of 2 and not on the banned list.

Input Format:

The first line of input contains a string PARAGRAPH.
The second line of input contains an integer 'N' representing the size of the banned words list.
The third line of input contains 'N' space-separated words BANNEDWORDS.

Output format:

Print the word (in UPPERCASE) which is most frequent and is not present in the list of banned words.

Note :

You don’t have to print anything. It has already been taken care of. Just implement the given function. 

Constraints:

1<= N <=10^6
1<=banned.size<=100
Where ‘N’ is the size of paragraph.

Time limit: 1 second
CodingNinjas
author
2y
Brute Force
  1. To find the frequency of each word we need to extract the words from the paragraph.
  2. So we will convert all the punctuation to space in the paragraph and lowercase alphabets to uppercase.
  3. Now ...read more
CodingNinjas
author
2y
Hashing

First of all, we need to extract all words into an array of strings. We can do this the same way as we did in the earlier approach. But this time we will use Hashmap to store the frequency of e...read more

CodingNinjas
author
2y
Improved Hashing

We can solve this problem without extracting words into extra space and without storing Banned into Set. We will only be using a Hashmap to store the frequency of words and will initia...read more

Janavi Paratane
3mo
Steps: Normalize the Input: Convert all the words in the paragraph to lowercase to avoid case-sensitivity issues. Remove punctuation like commas, periods, etc., so that only the words remain. Split th...read more
Add answer anonymously...
Josh Technology Group Web Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions
Get AmbitionBox app

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter