LeetCode 49. Group Anagrams

Given an array of strings, group anagrams together.


Input: ["eat", "tea", "tan", "ate", "nat", "bat"],


  • All inputs will be in lowercase.
  • The order of your output does not matter.

So call anagram means strings have the same characters, but at different order, like “ate”, “eat” and “tea”. This problem is some kind of group problem.

First, we need a way to check strings are anagrams or not. My method is to sort string to a unified form. Like “ate”, “eat” and “tea”, after to sort them, they will all be “aet”, so we know they are anagrams. Then we use the sorted result as the key of the hashmap, use a list as the value of hashmap. Then we use one loop to finish a count. Finally, we just output the content of hashmap.


To see more hashtable related problems, see 
Problems and Solutions of Hashmap.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.