Group Anagrams
Problem statement
You are given an array of strings strs. Two strings are anagrams when one can be rearranged into the other, which means they contain the same letters, each the same number of times.
Split the strings into groups so that two strings are in the same group exactly when they are anagrams, and return the groups.
Return the groups in a fixed order: the groups appear in the order in which their first string appears in strs, and inside a group the strings keep the order they have in strs. If a string appears more than once in strs, every copy is kept. The empty string is an anagram only of another empty string.
Function
groupAnagrams(strs: String[]) → String[][]Examples
Example 1
strs = ["listen","google","silent","enlist","elgoog","cat"]return = [["listen","silent","enlist"],["google","elgoog"],["cat"]]"listen", "silent" and "enlist" use the same six letters, and "google" and "elgoog" use the same six letters. "cat" is alone. The group of "listen" comes first because "listen" is the first string, then the group of "google", then "cat". Inside each group the strings keep their order from strs.
Example 2
strs = ["","b","","bb"]return = [["",""],["b"],["bb"]]The two empty strings are anagrams of each other, so they form one group. "b" and "bb" use the same letter but a different number of times, so they are in different groups.
Example 3
strs = ["abc","bca","abc","cab","ab"]return = [["abc","bca","abc","cab"],["ab"]]"abc" appears twice, and both copies are kept in its group, in their places from strs. "ab" is missing the letter c, so it is in a group of its own.
Constraints
1 <= strs.length <= 10000 <= strs[i].length <= 100- The total length of all strings in
strsis at most5000. strs[i]contains only lowercase English letters.