Anagram Words in a Sentence
Updated
Problem statement
You are given a string sentence made of words separated by single spaces. Each word is made of lowercase English letters.
Two different words are anagrams of each other if one can be turned into the other by reordering its letters. In other words, they use exactly the same letters, the same number of times each. For example, stop and pots are anagrams, but aab and abb are not.
Find the words of the sentence that are anagrams of each other, and return them in groups:
- Each group holds words that are all anagrams of one another, and it must contain at least two different words. A word with no anagram in the sentence is left out.
- A word that appears more than once is listed only once. A word is not an anagram of itself, so repeating a word does not make a group.
- Inside a group, list the words in the order of their first appearance in the sentence.
- Order the groups by where their first word first appears in the sentence.
If no two different words are anagrams, return an empty list.
Function
findAnagramGroups(sentence: String) → List<List<String>>Examples
Example 1
sentence = "the night shift thing pots stop tops ends here"return = [["night","thing"],["pots","stop","tops"]]night and thing both use the letters g, h, i, n, t once each, so they form a group. pots, stop and tops all use o, p, s, t once each, so they form a second group, listed in the order they appear.
The other words (the, shift, ends, here) have no partner. The group with night comes first because night appears before pots.
Example 2
sentence = "listen to the silent stone notes as onset enlist"return = [["listen","silent","enlist"],["stone","notes","onset"]]listen, silent and enlist all use e, i, l, n, s, t once each. stone, notes and onset all use e, n, o, s, t once each.
to, the and as have no partner. listen is the first word of the sentence, so its group comes first.
Example 3
sentence = "we saw a cat and a dog"return = []No two different words here use the same letters. The word a appears twice, but a word is not an anagram of itself, so the two copies do not form a group. The answer is an empty list.
Constraints
1 <= sentence.length <= 10^4sentencecontains only lowercase English letters and spaces.- Words are separated by exactly one space, and there is no space at the start or end.
More Nutanix Onsite Interview questions
- Design Hit CounterONSITE INTERVIEW · Seen Oct 2026
- Peak Process MemoryONSITE INTERVIEW · Seen Aug 2026
- Minimum Operations to Transform ArrayONSITE INTERVIEW · Seen Jun 2026
- Count Connected Components in an Undirected GraphONSITE INTERVIEW · Seen Aug 2025
- LRU CacheONSITE INTERVIEW · Seen Aug 2025
- Running Median of a Multilevel Linked List in Level OrderONSITE INTERVIEW · Seen Aug 2025
- Custom Array IteratorONSITE INTERVIEW · Seen Dec 2024
- Collapse Runs of Repeated CharactersONSITE INTERVIEW · Seen Nov 2022
- Sudoku SolverONSITE INTERVIEW · Seen Feb 2022
- Distance Sums to Special Nodes in a Weighted TreeONSITE INTERVIEW · Seen Aug 2021
Similar questions at other companies
- Count Adjacent Symbol Pairs for BPEAmazon · ONSITE INTERVIEW
- Map and Accumulate Learning QueriesPalantir · ONSITE INTERVIEWPalantir interview questions
- Filter Anagrams of a TargetMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Deduplicate Logs: Keep FirstGoogle · ONSITE INTERVIEWGoogle interview questions
- Check AnagramsDeloitte · ONSITE INTERVIEWDeloitte interview questions
- Check AnagramsBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Check Anagram Pairs Index by IndexExpedia · ONSITE INTERVIEWExpedia interview questions
- Recursive Character FrequencyPalo Alto Networks · ONSITE INTERVIEWPalo Alto Networks interview questions
- Valid AnagramMotive · ONSITE INTERVIEWMotive interview questions
- List Each Name Once, AlphabeticallyDocuSign · ONSITE INTERVIEWDocuSign interview questions