-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmorseCode.js
More file actions
28 lines (22 loc) · 924 Bytes
/
Copy pathmorseCode.js
File metadata and controls
28 lines (22 loc) · 924 Bytes
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
// Write a function that translates an array of strings into morse code and
// count how many different words are there in morse code.
const morse = [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]
const alph = 'abcdefghijklmnopqrstuvwxyz'.split('')
let uniqueMorseRepresentations = (words) => {
let storage = new Set()
let keys = {}
for (let i = 0; i < alph.length; i++) {
keys[alph[i]] = morse[i]
}
for (let i = 0; i < words.length; i++) {
let word = ''
for (let j = 0; j < words[i].length; j++) {
word += keys[words[i][j]]
}
storage.add(word)
word = ''
}
return storage.size
}
// This solves it in O(n + m) as it goes through the entire words array (n) and the entire each word (m). Faster than using
// indexOf as it doesn't have to loop to find the index.