This is another one of those problems that I was able to solve in less than 5 minutes.
class Solution {
var kbChars = arrayOf(
"qwertyuiop",
"asdfghjkl",
"zxcvbnm")
fun findWords(words: Array<String>): Array<String> {
return words.filter { word ->
var result = false
kbChars.forEach { chars ->
result = result or word.all {
it.toLowerCase() in chars
}
}
result
}.toTypedArray()
}
}
Runtime: 132 ms, faster than 100.00% of Kotlin online submissions for Keyboard Row.
Memory Usage: 32.7 MB, less than 100.00% of Kotlin online submissions for Keyboard Row.
It’s a straightforward implementation and I don’t think I need to explain the solution in details.