Arrays and Hashing Hash map lookups

61. Group Anagrams

Medium 147 DSA Sheet

You are given a list of lowercase words, words. Two words are anagrams if one can be rearranged into the other: they use the same letters, and each letter the same number of times.

Split the words into groups so that two words share a group exactly when they are anagrams, and return the groups. Every word belongs to exactly one group, and a word that appears twice in words appears twice in its group.

The groups can come back in any order, and so can the words inside each group. The examples list each group's words alphabetically and sort the groups by their first word.

Example 1

Input: words = ["pat", "rot", "tap", "sun", "tor", "apt"]

Output: [["apt","pat","tap"],["rot","tor"],["sun"]]

Explanation: "pat", "tap" and "apt" each use a, p and t once. "rot" and "tor" each use o, r and t once. "sun" has no anagram in the list, so it forms a group on its own.

Example 2

Input: words = ["dog", "god", "dog", "cat"]

Output: [["cat"],["dog","dog","god"]]

Explanation: "dog" appears twice, and both copies go into the group with "god". "cat" is alone.

Example 3

Input: words = ["aab", "aba", "abb", "bab", "b"]

Output: [["aab","aba"],["abb","bab"],["b"]]

Explanation: "aab" and "abb" use the same two letters, but "aab" has two a's and "abb" has two b's, so they are not anagrams. "b" has the right letter but not enough of them.

Check your understanding

Work out the answer for this input before you write any code.

words = ["ab", "ba", "aab"]

Constraints

  • 1 ≤ words.length ≤ 104
  • 1 ≤ words[i].length ≤ 100
  • words[i] holds only lowercase English letters

Clarifying questions

What you would ask an interviewer before writing any code.

Hints

Interview follow-ups

Runs in your browser
Code
Runs in your browser for now · some things may not work as expectedLn 1, Col 1

Sign in to oneby7

Keep your progress, your streak and your code, on any device.

New here? This creates your account too.