problems: add „804. Unique Morse Code Words“
Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
parent
fa54cd1335
commit
3b7f75f4e8
1 changed files with 47 additions and 0 deletions
47
problems/unique-morse-code-words.kt
Normal file
47
problems/unique-morse-code-words.kt
Normal file
|
@ -0,0 +1,47 @@
|
|||
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)
|
||||
}
|
Loading…
Reference in a new issue