LeetCode: Group Anagrams Solution

Implementation

1var groupAnagrams = function (strs) {
2 const sortStr = str => str.split("").sort().join("")
3 return [
4 ...strs
5 .reduce((acc, el) => {
6 let sortedStr = sortStr(el)
7 if (!acc.has(sortedStr)) {
8 acc.set(sortedStr, [el])
9 } else {
10 acc.get(sortedStr).push(el)
11 }
12 return acc
13 }, new Map())
14 .values(),
15 ]
16}

References

Original problem

Similar problems

Valid Anagram

Group Shifted Strings

Find Resultant Array After Removing Anagrams

Comments

Loading comments...

Tags

leetcode

hash table

string

Next Post

LeetCode: Peeking Iterator

HoningJS

Search Posts