1
0
Fork 0
mirror of https://gitlab.com/mfocko/LeetCode.git synced 2024-09-19 17:56:55 +02:00
LeetCode/problems/kt/unique-morse-code-words.kt
Matej Focko 333866d1bc
chore: split solutions by language
Signed-off-by: Matej Focko <mfocko@redhat.com>
2023-06-02 17:19:02 +02: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)
}