Coding questions · Voleon
Return Words in Repeated Anagram Groups
Return all input occurrences belonging to repeated anagram groups, preserving order and duplicates with multiplicity-aware letter-count signatures.
Given a list of words, return every word whose anagram signature occurs more than once in the list. A signature records how many times each letter occurs, regardless of letter order.
Implement repeated_anagram_words(words: string[]) -> string[].
Constraints & Assumptions
- At most 100000 words and at most 1000000 letters in total. Words contain lowercase ASCII letters only; empty words are allowed.
- Return qualifying occurrences in their original input order. Do not sort words or deduplicate the output.
- Identical repeated words count as multiple occurrences of their signature. Two empty words also share a repeated signature.
- A signature must preserve multiplicity:
abandaabare different. - Aim for O(total letters + number of words) time using a fixed-size letter-count signature per word.
Examples
words = ["eat","tea","bat","eat","tan","nat","solo"]
result = ["eat","tea","eat","tan","nat"]
words = ["a","ab","aab",""]
result = []
The output order and duplicate policy are explicit practice conventions where the source leaves them unspecified. Explain why a set of distinct letters is insufficient and how a count-signature avoids sorting every word.
Hint: Count signatures before selecting occurrences
Whether the first word qualifies may depend on a later word. Keep the original ordering separate from the signature frequency table.
Return Original Word Occurrences with Repeated Anagram Signatures
Approach
Represent a word by its 26 letter multiplicities. Equal count tuples are equivalent to being anagrams because order is irrelevant but every letter occurrence is preserved. A mere set of letters would wrongly merge ab with aab. First count signature frequencies over all occurrences, including duplicates and empty words. Then scan the original list again, emitting each word whose signature frequency exceeds one. This retains order and duplicate occurrences, and lets a later anagram qualify an earlier word. Recomputing signatures on the second scan avoids storing a separate key per input occurrence. With L total letters and N words, two fixed-alphabet counting passes take expected O(L+N) time. State stores one 26-count signature per distinct class plus frequencies and a temporary counter. Textual signatures in Java/JavaScript/C++ use separators to make the count encoding unambiguous; their character storage depends on count digit lengths. No word sorting is required.
- Time complexity: O(total letters + number of words) expected
- Space complexity: O(26 * distinct signatures) count information, plus encoded-key characters and output