Skip to content

Commit 27ccb41

Browse files
committed
Add solution #804
1 parent 333bd36 commit 27ccb41

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* 804. Unique Morse Code Words
3+
* https://leetcode.com/problems/unique-morse-code-words/
4+
* Difficulty: Easy
5+
*
6+
* International Morse Code defines a standard encoding where
7+
* each letter is mapped to a series of dots and dashes, as
8+
* follows: "a" maps to ".-", "b" maps to "-...",
9+
* "c" maps to "-.-.", and so on.
10+
*
11+
* For convenience, the full table for the 26 letters of the
12+
* English alphabet is given below:
13+
*
14+
* [".-","-...","-.-.","-..",".","..-.","--.","....","..",
15+
* ".---","-.-",".-..","--","-.","---",".--.","--.-",".-.",
16+
* "...","-","..-","...-",".--","-..-","-.--","--.."]
17+
*
18+
* Now, given a list of words, each word can be written as a
19+
* concatenation of the Morse code of each letter.
20+
* For example, "cba" can be written as "-.-..--...",
21+
* (which is the concatenation "-.-." + "-..." + ".-").
22+
* We'll call such a concatenation, the transformation of a word.
23+
*
24+
* Return the number of different transformations
25+
* among all words we have.
26+
*/
27+
28+
/**
29+
* @param {string[]} words
30+
* @return {number}
31+
*/
32+
var uniqueMorseRepresentations = function(words) {
33+
const codes = [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---",
34+
"-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-",
35+
"..-","...-",".--","-..-","-.--","--.."];
36+
const map = new Map(codes.map((v, i) => [String.fromCharCode(97 + i), v]));
37+
const transformed = words.map(word => word.split('').map(c => map.get(c)).join(''));
38+
return new Set(transformed).size;
39+
};

0 commit comments

Comments
 (0)