Ads Top

Interview Practice: Javascript Group Anagrams Solution

Anagram : a word, phrase, or name formed by rearranging the letters of another, such as cinema, formed from iceman.

Previously, I posted a solution for finding a single anagram within a word, you can see that solutiuon here

A step up from solving for a single anagram is to group anagrams together. For example, given the following input, this function should presetnt the output give below :

Input: strs = ["eat","tea","tan","ate","nat","bat"]
Output: [["bat"],["nat","tan"],["ate","eat","tea"]]

The solution for this problem is a combination of two other problems. A combination of the TwoSum problem and the basic anagram finder problem can get you to a solution for the group anagram problem.

Key mehtods for solving this problem
a hash for storing and tracking words (Js object)
split, sort, join method used in original anagram problem

Here is the code to implement this solution:



Leetcode Group Anagram

No comments:

Powered by Blogger.