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.
Be the first one to answer
Add answer anonymously...
ExpertLancing Research Services Software Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter