Anagrams Problem Statement
You are given an array/list of strings called inputStr
. Your task is to return the strings as groups of anagrams such that strings belonging to the same group are anagrams of each other.
Explanation:
An anagram is a word or phrase formed by rearranging the letters of a different word or phrase. In terms of string processing, an anagram of a string is another string with exactly the same quantity of each character in any order.
Note:
The order in which the groups and their members are printed does not matter.
Example:
Input:
inputStr = {"eat","tea","tan","ate","nat","bat"}
Output:
{{“eat”, “tea”, “ate”}, {“tan”, “nat”}, {“bat”}}
Explanation:
Here {“eat”, “tea”, “ate”} and {“tan”, “nat”} are grouped as anagrams. Since there is no string in “inputStr” which can be an anagram of “bat”, “bat” will be the only member in its group.
Input:
The first line contains an integer 'T', which indicates the number of test cases to be run. The test cases follow. The first line of each test case contains an integer 'N', which specifies the number of strings. The next line contains 'N' space-separated strings composed of lowercase English alphabets only.
Output:
For each test case, print the anagrams in the same group in a single line, with each anagram separated by a single space. Each group should be printed on a separate line. The outputs for each test case should be separated by a blank line.
Constraints:
- 1<= T <= 50
- 1<= N <= 100
- 1<= K <= 10
Where 'T' denotes the number of test cases, 'N' represents the number of strings in the given array/list, and 'K' is the maximum length of a string in the list.
Time limit: 1 sec.
Note:
You do not need to print anything, as output handling is already managed. Implement the function to return the solution.
Popular interview questions of Software Developer
Reviews
Interviews
Salaries
Users/Month