Group Words by Their Letters
Problem statement
Two words are anagrams when one is a rearrangement of the other: they use the same letters, each the same number of times. Put the words of words into groups so that each group holds all the words that are anagrams of each other.
Return the groups in the order in which their first word appears in words. Inside a group, keep the words in input order, including repeated copies.
Function
groupAnagrams(words: String[]) → List<List<String>>Examples
Example 1
words = ["listen","stone","silent","notes","tinsel","onset","angel"]return = [["listen","silent","tinsel"],["stone","notes","onset"],["angel"]]listen, silent and tinsel use the same six letters; stone, notes and onset share another five. angel matches nothing. Groups follow the order in which their first word appears.
Example 2
words = ["aab","abb","bab","baa"]return = [["aab","baa"],["abb","bab"]]aab and baa have two a's and one b; abb and bab have one a and two b's. Having the same set of letters is not enough: the counts must match too.
Example 3
words = ["go","og","go"]return = [["go","og","go"]]The same word may appear twice; every copy stays in the group, in input order.
Constraints
1 <= words.length <= 1500- each word is 1 to 10 lowercase English letters; the same word may appear more than once