Practice

Problem 5 of 12

Problem · Medium · 25 min

Group the anagrams

  • Medium
  • Hash maps
  • Strings
  • Sorting

Two words are anagrams when they use the same letters the same number of times. For example, "listen" and "silent" are anagrams.

Write groupAnagrams(words). It takes an array of words and returns an array of groups. Each group is an array of words that are anagrams of each other.

  • The words use lower case letters only. No word is empty.
  • Order the groups by the first word of each group in the input.
  • Inside a group, keep the order of the input.
  • A word with no anagram makes a group of its own.
  • The same word can appear more than once. Keep every copy.

Examples

Call Result
groupAnagrams(['listen', 'silent', 'enlist', 'google', 'gogole', 'cat']) [['listen', 'silent', 'enlist'], ['google', 'gogole'], ['cat']]
groupAnagrams(['abc', 'bca', 'xyz']) [['abc', 'bca'], ['xyz']]
groupAnagrams([]) []

Tasks

2 to do

  1. Task 1, not checked yet. Make groupAnagrams pass the example tests.

  2. Task 2, not checked yet. Make groupAnagrams pass the hidden tests.

Task 1 of 2

Make groupAnagrams pass the example tests.

All tasks

main.js

Ctrl+Enter runs your code. Ctrl+Shift+Enter runs all the tests. Press Esc, then Tab, to leave the editor.

Output

Press Run to see your result

Your output shows here. Press Run to try your code.