3 minute read

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:

[”.-“,”-…”,”-.-.”,”-..”,”.”,”..-.”,”–.”,”….”,”..”,”.—”,”-.-“,”.-..”,”–”,”-.”,”—”,”.–.”,”–.-“,”.-.”,”…”,”-“,”..-“,”…-“,”.–”,”-..-“,”-.–”,”–..”] Given an array of strings words where each word can be written as a concatenation of the Morse code of each letter.

For example, “cab” can be written as “-.-..–…”, which is the concatenation of “-.-.”, “.-“, and “-…”. We will call such a concatenation the transformation of a word. Return the number of different transformations among all words we have.

Example 1:

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 “–…–.”. Example 2:

Input: words = [“a”] Output: 1

/**
 * @param {string[]} words
 * @return {number}
 */
var uniqueMorseRepresentations = function (words) {
  const morse = [
    ".-",
    "-...",
    "-.-.",
    "-..",
    ".",
    "..-.",
    "--.",
    "....",
    "..",
    ".---",
    "-.-",
    ".-..",
    "--",
    "-.",
    "---",
    ".--.",
    "--.-",
    ".-.",
    "...",
    "-",
    "..-",
    "...-",
    ".--",
    "-..-",
    "-.--",
    "--..",
  ];
  // Create a map to store the morse code representation of each letter.
  let map = new Map();
  // Initialize the ASCII value of 'a' to 97.
  let ascii = "a".charCodeAt(0);
  // Iterate through the morse array and map each letter to its morse code representation.
  morse.forEach((m) => map.set(ascii++, m));

  // Create a set to store unique morse code representations of the words.
  let set = new Set();
  // Iterate through each word in the words array.
  for (const word of words) {
    // Initialize an empty string to build the morse code representation of the current word.
    let result = "";
    // Iterate through each character in the word.
    for (const w of word) {
      // Look up the morse code representation of the current character from the map and append it to the result.
      let morseValue = map.get(w.charCodeAt(0));
      // Append the morse code representation of the current character to the result.
      result += morseValue;
    }
    // Add the morse code representation of the current word to the set.
    set.add(result);
  }
  // Return the size of the set, which represents the count of unique morse code representations.
  return set.size;
};

An array named morse is defined, where each element is a string representing the Morse code representation of a letter from ‘a’ to ‘z’. This array is used to map characters to their Morse code equivalents.

A Map named map is created to map the ASCII values of lowercase letters (‘a’ to ‘z’) to their corresponding Morse code representations. It initializes by iterating through the morse array and mapping each ASCII value to the corresponding Morse code string.

A Set named set is created to store unique Morse code representations of the words.

The code then iterates through each word in the words array using a for...of loop.

Inside the loop, it initializes an empty string result to build the Morse code representation of the current word.

It iterates through each character (w) in the word using another for…of loop. For each character, it looks up its Morse code representation from the map using map.get(w.charCodeAt(0)) and appends it to the result.

After forming the Morse code representation for the current word, it is added to the set to keep track of unique representations.

Finally, the function returns the size of the set, which represents the count of unique Morse code representations.

The overall time complexity is O(n _ m), where “n” is the number of words, and “m” is the average length of a word in the words array. If “m” is relatively small compared to “n,” the time complexity can be approximated as O(n), but in a worst-case scenario where “m” is comparable to “n,” it could be O(n _ m).

Leave a comment