Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Form Anagram Groups

Medium Acceptance 69.80% Points 30.00

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.

Hints
Hint 1
Expected Time Complexity: O(n k m log m)
Hint 2
Expected Space Complexity: O(n*m)
Constraints
  • 1<= arr.size() <=100
  • 1<= arr[i].size() <=10
Companies
Amazon Microsoft Goldman Sachs DE Shaw Morgan Stanly
Topics
String Hashing
Solution.cs C#JavaPythonC++Javascript

Unlock the code editor

Sign in to write, run, and submit your solution against the full test suite.

  • Run code against sample & hidden test cases
  • Save submissions and track your streak
  • Compare with editorial & community solutions