> Uploading knowledge... _
[░░░░░░░░░░░░░░░░░░░░░░░░] 0%
blog logo
> CHICIO CODING_Pixels. Code. Unplugged.

Group Anagrams

Leetcode Problem 49: Group Anagrams

Problem Summary

Given an array of strings, group together all strings that are anagrams of each other (composed of the same characters with the same frequencies, in any order). Return the groups in any order.

Constraints:

  • The array has between 1 and 10,000 strings.
  • Each string has between 0 and 100 characters, consisting of lowercase English letters only.

Techniques

  • Array
  • Hash Table
  • String
  • Sorting

Solution

function groupAnagrams(strs: string[]): string[][] {
    let anagramGroups = new Map<string, string[]>()

    for (let i = 0; i < strs.length; i++) {
        let sorted = strs[i].split('').sort().join('')

        if (anagramGroups.has(sorted)) {
            anagramGroups.set(sorted, [...anagramGroups.get(sorted)!, strs[i]])
        } else {
            anagramGroups.set(sorted, [strs[i]])
        }
    }

    return Array.from(anagramGroups.values())
};

console.log(groupAnagrams(["eat","tea","tan","ate","nat","bat"]))