Loading...
You are given an array of strings words. Two words are anagrams if one can be formed by rearranging the letters of the other, meaning they contain exactly the same letters with the same frequencies. Two empty strings are anagrams of each other.
Return the indexes 0 to n - 1, where n is the length of words, partitioned into groups so that two indexes are in the same group exactly when their words are anagrams of each other.
The output must be in canonical order, which makes the answer unique:
words).Input: words = ["eat","tea","tan","ate","nat","bat"]
Output: [[0,1,3],[2,4],[5]]
Explanation: "eat" (0), "tea" (1), and "ate" (3) are anagrams of each other, so indexes 0, 1, 3 form the first group. "tan" (2) and "nat" (4) form the second group. No other word can be rearranged into "bat" (5), so index 5 is a group on its own. Groups appear in order of their smallest index: 0, then 2, then 5.
Input: words = [""]
Output: [[0]]
Explanation: The only word is the empty string, so index 0 forms the only group.
Input: words = ["a"]
Output: [[0]]
Explanation: A single word always forms a single group.
words.length ≤104words[i].length ≤100words[i] consists of lowercase English letters.Click "Run" to test with sample cases or "Submit" to run all tests.