Given a string S in the form of a sentence, the task is to find the word from the text with maximum number of its anagrams present in the given sentence.
Input: S = “please be silent and listen to what the professor says”
Only the word “silent” has an anagram(listen) present in the sentence.
Input: S = “cat is not a dog and sword has no words when government creates act so what is tac”
The word “cat” has two anagrams (“act”, “tac”) present in the sentence.
The word “words” has an anagram (“sword”) present in the sentence.
Hence, the word with the maximum anagrams is “cat”.
Following observations need to be made to solve the program:
Property of Prime Numbers: The product of any combination of prime numbers generates an unique number which cannot be obtained by any other combination of prime numbers.
Follow the steps below to solve the problem:
- Assign each alphabet with a distinct prime number.
- For each word in the given string, calculate the product of prime numbers assigned to the characters of that word and store it in a HashMap.
- Calculate the product of prime numbers assigned to the characters of a word and store it in a HashMap.
- Find the product with maximum frequency in the HashMap and print one of the corresponding anagrams as the answer.
Below is the implementation of the above approach:
Time Complexity: O(N), N is the length of the string excluding the blankspaces.
Auxiliary Space: O(N)
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.
- Find the word from a given sentence having given word as prefix
- Frequency of smallest character in first sentence less than that of second sentence
- Java program to count the characters in each word in a given sentence
- C program to find and replace a word in a File by another given word
- Sums of ASCII values of each word in a sentence
- Program for length of the longest word in a sentence
- Print longest palindrome word in a sentence
- Program to replace a word with asterisks in a sentence
- Print each word in a sentence with their corresponding average of ASCII values
- Check if a word is present in a sentence
- C program to Replace a word in a text by another given word
- Longest Common Prefix using Word by Word Matching
- Given a sequence of words, print all anagrams together | Set 2
- Print all pairs of anagrams in a given array of strings
- Given a sequence of words, print all anagrams together using STL
- Given a sequence of words, print all anagrams together | Set 1
- Second most repeated word in a sequence
- Most frequent word in an array of strings
- Second most repeated word in a sequence in Python
- Most frequent word in first String which is not present in second 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.
Improved By : sapnasingh4991