Diwali Offer!Flat 20% OFF
Enroll Now
PrakalpanaLive online tech training

14. Group Anagrams

MediumDay 4 of 60Arrays & Hashing

Given an array of strings strs, group together the words that are anagrams of each other (they use exactly the same letters the same number of times, like "eat" and "tea"). Return the groups in any order, and the words inside a group in any order.

Example 1
Input: strs = ["eat","tea","tan","ate","nat","bat"]
Output: [["bat"],["nat","tan"],["ate","eat","tea"]]
"eat", "tea" and "ate" share the letters a, e, t. "tan" and "nat" share a, n, t.
Example 2
Input: strs = [""]
Output: [[""]]
Constraints
  • 1 <= strs.length <= 10^4
  • 0 <= strs[i].length <= 100
  • strs[i] has only lowercase English letters
Hints
Frequently asked at: Amazon, Meta, Microsoft, Uber, Goldman Sachs
Ctrl + Enter runs the examples

Not sure what to learn next?

A 1-on-1 session with an industry mentor and a roadmap built for you. How it works

  1. 1Discuss your goals
  2. 2Get a personal plan
  3. 3Start learning
Book for β‚Ή200/ 30 min