Swift solution - Hash Table


  • 0
    class Solution {
        func groupAnagrams(_ strs: [String]) -> [[String]] {
            if strs.count == 0 {
                return [[String]]()
            }
            
            var map = [String: [String]]()
            
            for str in strs {
                let key = String(Array(str.characters).sorted())
                if !map.keys.contains(key) {
                    map[key] = [String]()
                }
                map[key]?.append(str)
            }
            
            return Array(map.values)
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.