Update appNew update is available. Click here to update.
sidenav-btnClose
Topic list
Group Anagrams
MEDIUM
30 mins
13 upvotes
Strings
Topics (Covered in this problem)
Problem solved
Badge
Skill meter
Strings
-
-
Other topics
Problem solved
Badge
Skill meter
Matrices (2D Arrays)
-
-
Linked List
-
-
Sorting
-
-
Binary Search
-
-
Stacks & Queues
-
-
Trees
-
-
Graph
-
-
Dynamic Programming
-
-
Greedy
-
-
Tries
-
-
Arrays
-
-
SQL
-
-
Binary Search Trees
-
-
Heap
-
-
Bit Manipulation
-
-
Solve problems & track your progress
Checkout your overall progress in every topic here
Become
userLevel
Sensei
in DSA topics
Open the topic and solve more problems associated with it to improve your skills
Check out the skill meter for every topic
See how many problems you are left with to solve for cracking any stage. Score more than zero to get your progress counted.

Group Anagrams

Contributed by
Sounak Majumder
Medium
yellow-spark
0/80
Avg time to solve 30 mins
Success Rate 70 %
Share
13 upvotes

Problem Statement

You have been given an array/list of strings 'inputStr'. You are supposed to return the strings as groups of anagrams such that strings belonging to a particular group are anagrams of one another.

An anagram is a word or phrase formed by rearranging the letters of a different word or phrase. We can generalize this in string processing by saying that an anagram of a string is another string with the same quantity of each character in it, in any order.

Note:
The order in which the groups and members of the groups are printed does not matter.
For example:
inputStr = {"eat","tea","tan","ate","nat","bat"}
Here {“tea”, “ate”,” eat”} and {“nat”, “tan”} are grouped as anagrams. Since there is no such string in “inputStr” which can be an anagram of “bat”, thus, “bat” will be the only member in its group.
Detailed explanation ( Input/output format, Notes, Constraints, Images )
Sample Input 1:
2
4
abab baba aabb abbc
5
aecd bcda acbd abdc acda
Sample Output 1 :
aabb abab baba
abbc
abdc acbd bcda
acda
aecd
Explanation of the Sample Input 1 :
In the first test case, in the first group ["aabb", "abab", "baba"], all the strings are anagrams of one another and in the second group ["abbc"] has no anagram, so it's the only member in its group.

In the second test case, in the first group ["abdc", "acbd", "bcda"] all the strings are anagrams of one another, and in second and third group, both ["acda"] and ["aecd"] have no anagram, so they are the only member in their group 
Sample Input 2:
2
6
eat tea tan ate nat bat
5
cat dog tac god act
Sample Output 2 :
ate eat tea 
bat
nat tan
act cat tac
dog god
Explanation of the Sample Input 2 :
In the first test case, in the first group ["ate", "eat", "tea"] and the third group [“nat”, “tan”], all the strings are anagrams of one another and in the second group ["bat"] has no anagram, so it's the only member in its group and, 

In the second test case, in the first group ["act", "cat", "tac"] and in the second group ["dog", "god"], all the strings are anagrams of one another.
Reset Code
Full screen
Auto
copy-code
Console