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.
AnswerBot
4mo

Given an array of strings, group the strings that are anagrams of each other.

  • Iterate through the array of strings and sort each string alphabetically to create a key for grouping.

  • Use a hashmap to stor...read more

Help your peers!
Select
Add answer anonymously...
Thales Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

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

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits