Skip to content

Commit

Permalink
Update solution to the Group Anagrams
Browse files Browse the repository at this point in the history
  • Loading branch information
soapyigu committed Oct 6, 2022
1 parent f6c0b05 commit b734c5e
Showing 1 changed file with 1 addition and 9 deletions.
10 changes: 1 addition & 9 deletions String/GroupAnagrams.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,6 @@

class GroupAnagrams {
func groupAnagrams(_ strs: [String]) -> [[String]] {
var sortedStrToStrs = [String: [String]]()

for str in strs {
let sortedStr = String(str.sorted())

sortedStrToStrs[sortedStr, default: []].append(str)
}

return Array(sortedStrToStrs.values)
return Array(Dictionary(strs.map { (String($0.sorted()), [$0]) }, uniquingKeysWith: +).values)
}
}

0 comments on commit b734c5e

Please sign in to comment.