This question evaluates proficiency in data structures and algorithms for string grouping and lookup, testing skills in string manipulation, canonicalization/hashing concepts, memory-time trade-offs, and handling duplicates.
You are building a small in-memory service that stores words and can return all stored words that are anagrams of a query.
Design and implement a data structure AnagramStore supporting the following operations:
add(word: string) -> void
: Add a word to the store (duplicates may exist).
getAnagrams(query: string) -> list<string>
: Return
all
words previously added that are anagrams of
query
(including the word itself if it was added). The order of the returned list does not matter.
Two words are anagrams if they contain the same letters with the same frequencies (case-insensitive; assume input is lowercase a-z unless you state otherwise).
"eat"
,
"tea"
,
"tan"
,
"ate"
,
"nat"
,
"bat"
getAnagrams("ate")
can return:
["eat", "tea", "ate"]
getAnagrams("ant")
can return:
["tan", "nat"]
getAnagrams("tab")
can return:
["bat"]
10^5
total
add
operations
100