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

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

Help your peers!
Select
Add answer anonymously...

Infosys System Engineer interview questions & answers

A System Engineer was asked 2w agoQ. What is method overriding?
A System Engineer was asked 2w agoQ. What is the process for reversing a number?
A System Engineer was asked 2w agoQ. What are the details of Object-Oriented Programming (OOP) concepts?

Popular interview questions of System Engineer

A System Engineer was asked 2w agoQ1. What is the process for reversing a number?
A System Engineer was asked 2w agoQ2. What is method overriding?
A System Engineer was asked 2w agoQ3. What are the details of Object-Oriented Programming (OOP) concepts?

Top HR questions asked in Infosys System Engineer

A System Engineer was asked 2mo agoQ1. Explain your project.
A System Engineer was asked 3mo agoQ2. Explain your final semester project.
A System Engineer was asked 3mo agoQ3. What was your involvement in your college project?
Infosys System Engineer 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