DEV Community

Tee
Tee

Posted on • Updated on

Leetcode: "Unique Morse Code Words" Fun JavaScript One Line Solution ✨

This is part of my series where I explain approaches to solving coding problems. This is to help me articulate my thought process better, and inspire new problem solving approaches for developers!

Problem Statement:

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. 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.

Approach:
We need to return the number of morse representations for the given list of words. The approach for this problem is as follows:

  1. Look at each word in words.
  2. For each word, look at each letter.
  3. Get the morse representation of each letter
  4. Build the word with the morse letter
  5. Once we finished building the word, add it to our set if it isn't already in there
  6. Return the number of unique morse representations.

Solution:

const alphabet = {
    a: '.-', b: '-...',   c: '-.-.', d: '-..', e: '.', f: '..-.', g: '--.', h: '....', i: '..',  j: '.---',  k: '-.-',  l: '.-..', m: '--',
    n: '-.',  o: '---', p: '.--.',  q: '--.-',  r: '.-.', s: '...', t: '-', u: '..-', v: '...-', w: '.--', x: '-..-',  y: '-.--', z: '--..' 
}

/**
 * @param {string[]} words the word array to convert to morse
 * @return {number} the length of unique morse representations
 */
const uniqueMorseRepresentations = words => {  
    // PREVIOUS SOLUTION:
    // return [...new Set(words.map(word => word.split('').map(letter => alphabet[letter]).join('')))].length

    // Edited on 9-9-2019
    return new Set(words.map(word => word.split('').map(letter => alphabet[letter]).join(''))).size
}
Enter fullscreen mode Exit fullscreen mode

Explanation:
This is a fun problem to apply Array.map()! Since we're changing every element in the array, map() works nicely. We'll use the example words = ['gin', 'zen', 'gig', 'msg'] to explain how this works:

For each word, we'll split the characters of each word with our first use of map(). We'll have something like this after looking at one word:

[ ['g', 'i', 'n'], 'zen', 'gig', 'msg' ] 
Enter fullscreen mode Exit fullscreen mode

We'll use map() again inside our first map method. We'll do this to look at each letter of the inner array to create a Morse Code word.

For each letter during our second use of map(), we'll get the letter's Morse Code, and build a word with it using join(''). After one use of both map() methods, we'll get the first Morse word for 'gin':

['--...-.', 'zen', 'gig', 'msg'] 
Enter fullscreen mode Exit fullscreen mode

We'll repeat steps 1-3 until we have an array of new Morse Code words:

['--...-.', '--...-.', '--...--.', '--...--.']
Enter fullscreen mode Exit fullscreen mode

We'll use JavaScript's Set Object to only keep the unique representations. The Set constructor takes in an array, which will be the array shown in Step 2, and then removes duplicate elements, so we'll only be left with unique Morse Code words:

['--...-.', '--...--.']
Enter fullscreen mode Exit fullscreen mode

Finally, we can return the size of the set like so:

return new Set(words.map(word => word.split('').map(letter => alphabet[letter]).join(''))).size
Enter fullscreen mode Exit fullscreen mode



Thanks for reading! As always questions, feedback, and ideas are always encouraged. Happy hacking!

Top comments (0)