Skip to content

Commit f831fdf

Browse files
authored
Create 2325-decode-the-message.js
1 parent a45e6b0 commit f831fdf

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

2325-decode-the-message.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* @param {string} key
3+
* @param {string} message
4+
* @return {string}
5+
*/
6+
var decodeMessage = function(key, message) {
7+
const set = new Set()
8+
for(const ch of key) {
9+
if(ch !== ' ') set.add(ch)
10+
if(set.size === 26) break
11+
}
12+
const arr = Array.from(set).map((e, i) => [e, i])
13+
const hash = {}
14+
for(const [e, i] of arr) {
15+
hash[e] = i
16+
}
17+
// console.log(arr)
18+
const a = 'a'.charCodeAt(0)
19+
let res = ''
20+
for(const ch of message) {
21+
if(ch === ' ') {
22+
res += ' '
23+
continue
24+
}
25+
const idx = hash[ch]
26+
const tmp = String.fromCharCode(a + idx)
27+
res += tmp
28+
}
29+
30+
return res
31+
};

0 commit comments

Comments
 (0)