g0001_0100.s0049_group_anagrams.Solution.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-kotlin Show documentation
Show all versions of leetcode-in-kotlin Show documentation
Kotlin-based LeetCode algorithm problem solutions, regularly updated
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
// #2022_08_29_Time_506_ms_(86.55%)_Space_72.5_MB_(81.04%)
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[temp]!!.add(s)
}
return ArrayList>(hm.values)
}
}