LeetCode/kt/unique-morse-code-words.kt
Matej Focko 2351dfd0ee
chore: unwrap one layer
Signed-off-by: Matej Focko <mfocko@redhat.com>
2023-12-12 14:36:00 +01:00

47 lines
924 B
Kotlin

class Solution {
val mapping = arrayOf(
".-",
"-...",
"-.-.",
"-..",
".",
"..-.",
"--.",
"....",
"..",
".---",
"-.-",
".-..",
"--",
"-.",
"---",
".--.",
"--.-",
".-.",
"...",
"-",
"..-",
"...-",
".--",
"-..-",
"-.--",
"--.."
)
private fun charToMorse(c: Char): String = mapping[c - 'a']
fun uniqueMorseRepresentations(words: Array<String>): Int =
words
.map { word ->
word.map { charToMorse(it) }.joinToString(separator = "")
}
.toSet()
.size
}
fun main() {
val s = Solution()
check(s.uniqueMorseRepresentations(arrayOf("gin", "zen", "gig", "msg")) == 2)
check(s.uniqueMorseRepresentations(arrayOf("a")) == 1)
}