diff --git a/problems/unique-morse-code-words.kt b/problems/unique-morse-code-words.kt new file mode 100644 index 0000000..e65bf97 --- /dev/null +++ b/problems/unique-morse-code-words.kt @@ -0,0 +1,47 @@ +class Solution { + val mapping = arrayOf( + ".-", + "-...", + "-.-.", + "-..", + ".", + "..-.", + "--.", + "....", + "..", + ".---", + "-.-", + ".-..", + "--", + "-.", + "---", + ".--.", + "--.-", + ".-.", + "...", + "-", + "..-", + "...-", + ".--", + "-..-", + "-.--", + "--.." + ) + + private fun charToMorse(c: Char): String = mapping[c - 'a'] + + fun uniqueMorseRepresentations(words: Array): 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) +}