-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuniqueMorseCodeWords.ts
40 lines (38 loc) · 1.2 KB
/
uniqueMorseCodeWords.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
function uniqueMorseRepresentations(words: string[]): number {
// create a map to assign alphabet letters to morse code
let dict = {
'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': "--..",
}
let ans: string[] = []
// iterate over the array. Step into each element, iterate over the string. Replace the string with its morse code counter part. Join the string into its morse code equivalent. Step into the next element and repeat. Extract the number of unique morse code and return the size/length.
for (let i = 0; i < words.length; i++) {
// iterate over the string, replacing the current words with their morse counterparts
ans.push(words[i].split('').map( x => dict[x]).join(''))
}
return new Set(ans).size
};