Skip to content

Commit 610eaae

Browse files
author
Thuy Trinh
committed
Solution for "Group Anagrams"
1 parent b066761 commit 610eaae

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package leetcode.groupanagrams
2+
3+
/**
4+
* https://leetcode.com/problems/group-anagrams/description/
5+
*/
6+
class Solution {
7+
fun groupAnagrams(strs: Array<String>): List<List<String>> {
8+
val map = mutableMapOf<List<Char>, MutableList<String>>()
9+
strs.forEach {
10+
val v = map.getOrPut(it.toList().sorted()) {
11+
mutableListOf()
12+
}
13+
v += it
14+
}
15+
return map.values
16+
.map { it.sorted() }
17+
}
18+
}

src/main/java/leetcode/lettercombinations/Solution.kt

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ package leetcode.lettercombinations
22

33
import java.util.*
44

5+
/**
6+
* https://leetcode.com/problems/letter-combinations-of-a-phone-number/description/
7+
*/
58
class Solution {
69
private val kb = hashMapOf(
710
'1' to "",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package leetcode.groupanagrams
2+
3+
import org.amshove.kluent.shouldContainAll
4+
import org.junit.Test
5+
6+
class SolutionTest {
7+
@Test
8+
fun groupAnagrams() {
9+
Solution().groupAnagrams(arrayOf("eat", "tea", "tan", "ate", "nat", "bat"))
10+
.shouldContainAll(listOf(
11+
listOf("ate", "eat", "tea"),
12+
listOf("nat", "tan"),
13+
listOf("bat")
14+
))
15+
}
16+
}

0 commit comments

Comments
 (0)