Description

Given an array of strings strs, group the anagrams together. You can return the answer in any order. An Anagram is a word formed by rearranging the letters of a different word, using all the original letters exactly once.

Examples

Input:strs = ["eat","tea","tan","ate","nat","bat"]
Output:[["bat"],["nat","tan"],["ate","eat","tea"]]
Explanation:

Sorting each word alphabetically gives: eat->aet, tea->aet, tan->ant, ate->aet, nat->ant, bat->abt. Words with the same sorted form are anagrams: 'aet' group contains [eat,tea,ate], 'ant' group contains [tan,nat], 'abt' group contains [bat].

Input:strs = [""]
Output:[[""]]
Explanation:

An empty string has no characters to sort, so its sorted key is also empty. Since there is only one empty string, it forms a single-element group by itself.

Input:strs = ["a"]
Output:[["a"]]
Explanation:

The single character 'a' sorted is still 'a'. With no other strings sharing this key, 'a' forms its own group containing just itself.

Constraints

  • 1 ≤ strs.length ≤ 10⁴
  • 0 ≤ strs[i].length ≤ 100
  • strs[i] consists of lowercase English letters.

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!