Find Anagrams
EasyPython 3StringsHash tablesSorting
You are given a list of strings words. Two words are anagrams of each other when one can be rearranged into the other (same multiset of characters).
Return the list of words that have at least one anagram elsewhere in the list. Preserve the original input order and keep duplicates (a word is included once for each time it appears, as long as its anagram group has more than one word).
Constraints: - 0 <= len(words) <= 10^4 - each word consists of lowercase English letters
Examples:
["tea", "ate", "eat", "apple", "java", "vaja", "cut", "utc"]
-> ["tea", "ate", "eat", "java", "vaja", "cut", "utc"] ("apple" is alone)["abc", "def"] -> [] (no two words share an anagram group) [] -> [] ["ab", "ba", "ab"] -> ["ab", "ba", "ab"]
Python 3, run in your browser. Run tests runs the problem's own tests with a free account; the AI interviewer reads your code and your runs in a practice interview.
Loading the editor…