Bootcamps
Masterclasses
Menu
Courses
Account
Login / Sign Up

Form Anagram Groups

Medium Acceptance 69.8% Points 30

You are given an array of strings. Your task is to group all strings that are anagrams of each other. The groups should be formed in the order of their first appearance in the original array.

Note: The final output will be in lexicographic order.

Examples
Example 1

Example 1

Input: arr[] = ["act", "god", "cat", "dog", "tac"]

Output: [["act", "cat", "tac"], ["god", "dog"]]

Explanation: There are 2 groups of anagrams "god", "dog" make group 1. "act", "cat", "tac" make group 2

Example 2

Example 2

Input: arr[] = ["no", "on", "is"]

Output: [["is"],["no", "on"]]

Explanation: There are 2 groups of anagrams "is" makes group 1. "no", "on" make group 2.

Constraints
  • 1<= arr.size() <=100
  • 1<= arr[i].size() <=10
Hints
  • Expected Time Complexity: O(n k m log m)
  • Expected Space Complexity: O(n*m)
Topics
String Hashing
Companies
Amazon Microsoft Goldman Sachs DE Shaw Morgan Stanly
Added 19 Jul 2026 69.8% acceptance
Ln 1, Col 1Sign in to save your code
act god cat dog tac
act cat tac , god dog ,