From 3b7f75f4e832713dd7db03a06a3b580f510b323b Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Wed, 17 Aug 2022 11:48:21 +0200 Subject: [PATCH] =?UTF-8?q?problems:=20add=20=E2=80=9E804.=20Unique=20Mors?= =?UTF-8?q?e=20Code=20Words=E2=80=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matej Focko --- problems/unique-morse-code-words.kt | 47 +++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 problems/unique-morse-code-words.kt 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) +}