-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunique-morse-code-words_0311.html
97 lines (88 loc) · 2.68 KB
/
unique-morse-code-words_0311.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>unique-morse-code-words</title>
</head>
<body></body>
<script>
const words = ["gin", "zen", "gig", "msg"];
console.log(uniqueMorseRepresentations(words));
/**
* @param {string[]} words
* @return {number}
*/
// function uniqueMorseRepresentations(words) {
// const index = "a".charCodeAt(0);
// const letters = [
// ".-",
// "-...",
// "-.-.",
// "-..",
// ".",
// "..-.",
// "--.",
// "....",
// "..",
// ".---",
// "-.-",
// ".-..",
// "--",
// "-.",
// "---",
// ".--.",
// "--.-",
// ".-.",
// "...",
// "-",
// "..-",
// "...-",
// ".--",
// "-..-",
// "-.--",
// "--..",
// ];
// let result = [];
// words.forEach((word) => {
// let str = "";
// let arrWord = word.split("");
// for (let i = 0; i < word.length; i++) {
// str += letters[arrWord[i].charCodeAt(0) - index];
// }
// if (result.includes(str)) return;
// result.push(str);
// });
// return result.length;
// }
// WAY2
/**
* @param {string[]} words
* @return {number}
*/
function uniqueMorseRepresentations(words) {
const index = "a".charCodeAt(0);
const letters = [".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."];
// 1.将words使用map对每个word进行加工 words.map
// 2.将循环出来的每个word转换成array([...word]),并对里的字母进行加工[...word].map, 替换成letters里的code,再使用join('')将每个加工后的array连接成新的word
// 3. 使用new Set将加工后的words进行去重, 并使用size来返回去重后的长度
// console.log(
// new Set (
// words.map((word) => {
// return [...word].map((arg) => {
// return letters[arg.charCodeAt(0) - index]
// }).join('')
// })
// ).size
// );
return new Set(
words.map((word) =>
[...word].map((arg) =>
letters[arg.charCodeAt(0) - index]
).join('')
)
).size
}
</script>
</html>