words. Two strings are anagrams when one can be turned into the other by rearranging its letters, that is, both contain the same letters the same number of times.
Split the strings into groups so that every group holds strings that are anagrams of one another, and strings from different groups are not. Every element of the array, duplicates included, must land in exactly one group.
Return the list of groups. The groups, and the strings inside each group, may come in any order.words=['listen', 'google', 'silent', 'cat', 'enlist', 'act'] → [['listen', 'silent', 'enlist'], ['google'], ['cat', 'act']]
"listen", "silent" and "enlist" are made of the same letters, so are "cat" and "act", while "google" has no match and forms a group of its own.
words=['', 'b', ''] → [['', ''], ['b']]
The two empty strings are anagrams of each other and share a group; "b" stands alone.
words=['ab', 'ba', 'aab'] → [['ab', 'ba'], ['aab']]
"aab" has two a's, so it cannot join "ab" and "ba".
words.length ≤ 10⁴words[i].length ≤ 100words[i] consists of lowercase English letters onlywords=['listen', 'google', 'silent', 'cat', 'enlist', 'act']
[['listen', 'silent', 'enlist'], ['google'], ['cat', 'act']]
Run checks the first 3 tests in your browser. Submit checks all 71 tests on the server, hidden ones included.