-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSolution.js
112 lines (95 loc) · 3.83 KB
/
Solution.js
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// https://leetcode.com/problems/unique-morse-code-words/
// 10:30 AM
// 10:40 Am
// set faster than array
/**
* @param {string[]} words
* @return {number}
*/
//
/*
29%
*/
var uniqueMorseRepresentations1 = function (words) {
let code = { 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 arr = [];
for (let i = 0; i < words.length; i++) {
if (words.length == 1 && words[i].length == 1) return 1;
let morse = "";
for (let j = 0; j < words[i].length; j++) {
if (code[words[i][j]] != undefined) {
morse += code[words[i][j]];
}
}
if (!arr.includes(morse)) {
arr.push(morse);
}
}
return arr.length;
};
/*
Runtime: 79 ms, faster than 78.83% of JavaScript online submissions for Unique Morse Code Words.
Memory Usage: 42.7 MB, less than 86.68% of JavaScript online submissions for Unique Morse Code Words.
*/
var uniqueMorseRepresentations = function (words) {
let code = { 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 set = new Set();
for (let i = 0; i < words.length; i++) {
if (words.length == 1 && words[i].length == 1) return 1;
let morse = "";
for (let j = 0; j < words[i].length; j++) {
if (code[words[i][j]] != undefined) { // just in case (not required this checking)
morse += code[words[i][j]];
}
}
set.add(morse);
}
return set.size;
};
// optimized
// slower than ☝ previous solution
var uniqueMorseRepresentations = function (w) {
let c = { 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 s = new Set();
for (let i = 0; i < w.length; i++) {
if (w.length == 1 && w[i].length == 1) return 1;
let m = "";
for (let j = 0; j < w[i].length; j++) {
if (c[w[i][j]] != undefined) {
m += c[w[i][j]];
}
}
s.add(m);
}
return s.size;
};
let words = ["gin", "zen", "gig", "msg"];
words = ["a", "Sdsdss"];
console.log(uniqueMorseRepresentations(words));
// ANOTHER WAY
// 25-08-22
var generateKeys = function (codes) {
let keys = {};
let j = 0;
for (i = 97; i <= 122; i++) {
keys[String.fromCharCode(i)] = codes[j];
j++;
}
return keys;
};
let morseCode = [".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."];
var uniqueMorseRepresentations = function (words) {
// let code = { 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 code = generateKeys(morseCode);
let set = new Set();
for (let i = 0; i < words.length; i++) {
if (words.length == 1 && words[i].length == 1) return 1;
let morse = "";
for (let j = 0; j < words[i].length; j++) {
morse += code[words[i][j]];
}
set.add(morse);
}
return set.size;
};
console.log(uniqueMorseRepresentations("haihzj"));