Given an array of strings, group anagrams together.

Example:

Input: [“eat”, “tea”, “tan”, “ate”, “nat”, “bat”], Output: [ [“ate”,”eat”,”tea”], [“nat”,”tan”], [“bat”] ]

Note:

All inputs will be in lowercase.

The order of your output does not matter.

Solution

To group Anagram words, We can sort all string with characters and match it with other string to verify if its anagram or not. We can use Map to group all equal strings.

Code

Output

[[eat, tea, ate], [bat], [tan, nat]]

We encourage you to write a comment if you have a better solution or having any doubt on the above topic.

Leave a Reply

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