1
0
Fork 0
mirror of https://gitlab.com/mfocko/LeetCode.git synced 2024-09-16 16:36:56 +02:00
LeetCode/kt/unique-morse-code-words.kt
Matej Focko aaaebf1d52
style(kt): reformat the files
Signed-off-by: Matej Focko <me@mfocko.xyz>
2024-05-17 18:23:38 +02:00

48 lines
1 KiB
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)
}