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

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

There is a newer version: 1.30
Show newest version
package g0001_0100.s0049_group_anagrams

// #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) #2023_07_05_Time_308_ms_(96.34%)_Space_43.3_MB_(81.41%)

class Solution {
    fun groupAnagrams(strs: Array): List> {
        val hm: MutableMap> = HashMap()
        for (s in strs) {
            val ch = s.toCharArray()
            ch.sort()
            val temp = String(ch)
            hm.computeIfAbsent(
                temp
            ) { _: String? -> ArrayList() }
            hm.getValue(temp).add(s)
        }
        return ArrayList>(hm.values)
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy