Skip to content

Commit 4af8774

Browse files
authored
Merge pull request neetcode-gh#608 from sharansalian/main
Add 49-Group-Anagrams.kt
2 parents ee7b73f + 3732ea7 commit 4af8774

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

kotlin/49-Group-Anagrams.kt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package kotlin
2+
3+
fun main() {
4+
5+
val strs = arrayOf("eat", "tea", "tan", "ate", "nat", "bat")
6+
7+
println(groupAnagrams(strs))
8+
}
9+
10+
fun groupAnagrams(strs: Array<String>): List<List<String>> {
11+
val res: HashMap<String, MutableList<String>> = hashMapOf()
12+
13+
for (s in strs){
14+
val count = IntArray(26)
15+
16+
for (c in s){
17+
val index = c - 'a'
18+
count[index] += 1
19+
}
20+
21+
res[count.joinToString()] = res.getOrDefault(count.joinToString(), mutableListOf()).also { it.add(s) }
22+
}
23+
24+
return res.values.toList()
25+
}

0 commit comments

Comments
 (0)