LeetCode: Group Anagrams Solution
Implementation
1var groupAnagrams = function (strs) {2 const sortStr = str => str.split("").sort().join("")3 return [4 ...strs5 .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 acc13 }, new Map())14 .values(),15 ]16}
References
Similar problems
Group Shifted Strings
Find Resultant Array After Removing Anagrams
Comments
Loading comments...
Tags
leetcode
hash table
string
Apply and earn a $2,500 bonus once you're hired on your first job!
Clients from the Fortune 500 to Silicon Valley startups
Choose your own rate, get paid on time
From hourly, part-time, to full-time positions
Flexible remote working environment
A lot of open JavaScript jobs!!
Fact corner: Referred talent are 5x more likely to pass the Toptal screening process than the average applicant.
Still hesitate? Read HoningJS author's guide on dealing with Toptal interview process.