Group Anagrams Problem Statement
Given an array or list of strings called inputStr
, your task is to return the strings grouped as anagrams. Each group should contain strings that are anagrams of one another.
An anagram is a word or phrase formed by rearranging the letters of a different word or phrase. An anagram in string processing is defined as a string having the same quantity of each character in any order as another string.
Note:
The order of the groups and the order of the strings within each group does not matter.
Example:
Input:
inputStr = {"eat","tea","tan","ate","nat","bat"}
Output:
{“tea”, “ate”,” eat”}, {“nat”, “tan”}, {“bat”}
Explanation:
String sets {“tea”, “ate”,” eat”} and {“nat”, “tan”} are grouped as anagrams. The string “bat” has no other anagram in the input, so it forms a group on its own.
Input:
The first line contains an integer 'T', representing the number of test cases or queries. Each test case includes the following:
The first line of each test case contains an integer 'N' denoting the number of strings.
The next line contains 'N' space-separated strings made of lowercase English alphabets.
Output:
For each test case, print the anagrams belonging to the same group on a single line, separated by a space. Each group's anagrams are printed on a separate line.
The output for each test case must be printed on a new line.
Constraints:
- 1 <= T <= 50
- 1 <= N <= 100
- 1 <= K <= 10, where 'K' is the maximum length of a string in the given array/list.
Time limit: 1 second.
Note:
No need to print anything; the implementation should only focus on solving the task.
Group anagrams in a list of strings and return them as separate groups.
Iterate through the list of strings and sort each string to find anagrams.
Use a hashmap to group anagrams together based on their...read more
Top Infosys System Engineer interview questions & answers
Popular interview questions of System Engineer
Top HR questions asked in Infosys System Engineer
Reviews
Interviews
Salaries
Users/Month