Unique Morse Code Words
💻 coding

Unique Morse Code Words

2 min read 310 words
2 min read
ShareWhatsAppPost on X
  • 1International Morse Code maps each letter to a unique series of dots and dashes.
  • 2Words can be transformed into Morse code by concatenating the Morse representations of their letters.
  • 3The number of unique Morse code transformations among a list of words can be determined using a HashSet.

AI-generated summary · May not capture all nuances

Key Insight
AskGif

"International Morse Code maps each letter to a unique series of dots and dashes."

Unique Morse Code Words

International Morse Code defines a standard encoding where each letter is mapped to a series of dots and dashes, as follows: "a" maps to ".-", "b" maps to "-...", "c" maps to "-.-.", and so on.

For convenience, the full table for the 26 letters of the English alphabet is given below:

[".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]

Now, given a list of words, each word can be written as a concatenation of the Morse code of each letter. For example, "cba" can be written as "-.-..--...", (which is the concatenation "-.-." + "-..." + ".-"). We'll call such a concatenation, the transformation of a word.

Return the number of different transformations among all words we have.

Example:

Input: words = ["gin", "zen", "gig", "msg"]

Output: 2

Explanation: 

The transformation of each word is:

"gin" -> "--...-."

"zen" -> "--...-."

"gig" -> "--...--."

"msg" -> "--...--."

There are 2 different transformations, "--...-." and "--...--.".

Note:

The length of words will be at most 100.

Each words[i] will have length in range [1, 12].

words[i] will only consist of lowercase letters.

Solution:

using System;
using System.Collections.Generic;
using System.Text;

namespace NetCoreCoding.LeetCode.String.Easy
{
 public class UniqueMorseRepresentationsSoln
 {
 public UniqueMorseRepresentationsSoln()
 {
 }

 public void execute()
 {
 var words = new string[] { "gin", "zen", "gig", "msg" };
 var res = UniqueMorseRepresentations(words);
 Assert(res, 2);
 }

 private void Assert(int response, int expected)
 {
 if (response != expected)
 throw new Exception("Incorrect Answer.");
 }

 public int UniqueMorseRepresentations(string[] words)
 {
 var morse = new string[] { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.." };
 var set = new HashSet<string>();
 foreach (var word in words)
 {
 var str = new StringBuilder();
 for (int i = 0; i < word.Length; i++)
 {
 str.Append(morse[word[i]-'a']);
 }
 set.Add(str.ToString().Trim());
 }

 return set.Count;
 }
 }
}

Time Complexity: O(n^2)

Space Complexity: O(n)

Enjoyed this article?

Share it with someone who'd find it useful.

ShareWhatsAppPost on X

AskGif

Published on 8 May 2020 · 2 min read · 310 words

Part of AskGif Blog · coding

You might also like