 All Problems
Group Anagrams
medium
strings
hash map
sorting
amazon
facebook
google
microsoft

Given an array of strings strs, group the anagrams together. You can return the answer in any order.

Example 1:

Input:
6
eat tea tan ate nat bat
Output:
ate eat tea
bat
nat tan

Example 2:

Input:
1
a
Output:
a

Constraints:

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

Input format: First line: number of strings n. Second line: n space-separated strings.

Output format: Each anagram group on its own line with words sorted alphabetically; groups sorted by first word.

Run to check your code against the sample cases, or submit to run every case