All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g0001_0100.s0049_group_anagrams.Solution.swift Maven / Gradle / Ivy

There is a newer version: 1.8
Show newest version
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #String #Hash_Table #Sorting
// #Data_Structure_II_Day_8_String #Programming_Skills_II_Day_11 #Udemy_Strings
// #Big_O_Time_O(n*k_log_k)_Space_O(n) #2024_06_23_Time_67_ms_(84.64%)_Space_18.6_MB_(43.89%)

public class Solution {
    public func groupAnagrams(_ strs: [String]) -> [[String]] {
        var hm = [String: [String]]()
        for s in strs {
            let sortedStr = String(s.sorted())
            if hm[sortedStr] == nil {
                hm[sortedStr] = [String]()
            }
            hm[sortedStr]!.append(s)
        }
        return Array(hm.values)
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy