-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathSolution.kt
45 lines (39 loc) · 1.63 KB
/
Solution.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/**
* Created by Inno Fang on 2017/12/24.
*/
class Solution {
fun findWords(words: Array<String>): Array<String> {
val kr1 = hashSetOf('q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p')
val kr2 = hashSetOf('a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l')
val kr3 = hashSetOf('z', 'x', 'c', 'v', 'b', 'n', 'm')
val result = ArrayList<String>()
words.forEach {
when {
kr1.containsAll(it.toLowerCase().toHashSet()) ||
kr2.containsAll(it.toLowerCase().toHashSet()) ||
kr3.containsAll(it.toLowerCase().toHashSet()) -> result.add(it)
}
}
return result.toTypedArray()
}
}
class Solution2 {
fun findWords(words: Array<String>): Array<String> {
val kr1 = hashSetOf('q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p')
val kr2 = hashSetOf('a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l')
val kr3 = hashSetOf('z', 'x', 'c', 'v', 'b', 'n', 'm')
return words.filter {
if (kr1.contains(it[0].toLowerCase()) || kr2.contains(it[0].toLowerCase()) || kr3.contains(it[0].toLowerCase())) {
val word = it.toLowerCase().toHashSet()
(kr1.containsAll(word) || kr2.containsAll(word) || kr3.containsAll(word))
} else false
}.toTypedArray()
}
}
fun main(args: Array<String>) {
Solution2().findWords(arrayOf("Hello", "Alaska", "Dad", "Peace")).forEach { print("$it ") }
println()
Solution2().findWords(arrayOf("Error")).forEach { print("$it ") }
println()
Solution2().findWords(arrayOf("a", "b")).forEach { print("$it ") }
}