You get a vector strings of words.
Put the words that are anagrams of each other in the same group. Return the list of groups.
Two words are anagrams when they have the same letters, the same number of times. For example, "night" and "thing" are anagrams.
Every word goes in exactly one group. A word with no anagram is a group on its own.
The groups can come in any order. So can the words inside each group.
Example 1:
Input: strings = ["below","state","elbow","taste","bowel","star"] Output: [["below","elbow","bowel"],["state","taste"],["star"]] Explanation: There are three groups: "below", "elbow" and "bowel", "state" and "taste", "star" on its own, because it has no anagram. Other orders of the groups and words are right too.
Example 2:
Input: strings = ["ab","ba","ab"] Output: [["ab","ba","ab"]] Explanation: All three words are anagrams of each other. The repeated "ab" stays in the group twice.
Example 3:
Input: strings = ["moon","noon"] Output: [["moon"],["noon"]] Explanation: Both words have 4 letters. But "moon" has an m where "noon" has a second n. So each word is in a group of its own. [["noon"],["moon"]] is right too.
Constraints:
stringshas 1 to 10,000 words.- Each word has 1 to 22 lowercase English letters.
Note:
Your algorithm should run in O(n * L * log L) time and use O(n * L) extra space, where n is the number of strings and L is the length of the longest string.
Walkthroughs of the solution. Try the steps first, then watch to compare approaches.
Free plan: 30 tutor messages. PRO is unlimited.
