Skip to content

Commit 9dd0030

Browse files
committed
Add solution #2325
1 parent ce8272f commit 9dd0030

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,887 LeetCode solutions in JavaScript
1+
# 1,888 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1765,6 +1765,7 @@
17651765
2320|[Count Number of Ways to Place Houses](./solutions/2320-count-number-of-ways-to-place-houses.js)|Medium|
17661766
2321|[Maximum Score Of Spliced Array](./solutions/2321-maximum-score-of-spliced-array.js)|Hard|
17671767
2322|[Minimum Score After Removals on a Tree](./solutions/2322-minimum-score-after-removals-on-a-tree.js)|Hard|
1768+
2325|[Decode the Message](./solutions/2325-decode-the-message.js)|Easy|
17681769
2336|[Smallest Number in Infinite Set](./solutions/2336-smallest-number-in-infinite-set.js)|Medium|
17691770
2338|[Count the Number of Ideal Arrays](./solutions/2338-count-the-number-of-ideal-arrays.js)|Hard|
17701771
2342|[Max Sum of a Pair With Equal Sum of Digits](./solutions/2342-max-sum-of-a-pair-with-equal-sum-of-digits.js)|Medium|

solutions/2325-decode-the-message.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* 2325. Decode the Message
3+
* https://leetcode.com/problems/decode-the-message/
4+
* Difficulty: Easy
5+
*
6+
* You are given the strings key and message, which represent a cipher key and a secret message,
7+
* respectively. The steps to decode message are as follows:
8+
* 1. Use the first appearance of all 26 lowercase English letters in key as the order of the
9+
* substitution table.
10+
* 2. Align the substitution table with the regular English alphabet.
11+
* 3. Each letter in message is then substituted using the table.
12+
* 4. Spaces ' ' are transformed to themselves.
13+
* 5. For example, given key = "happy boy" (actual key would have at least one instance of each
14+
* letter in the alphabet), we have the partial substitution table of ('h' -> 'a', 'a' -> 'b',
15+
* 'p' -> 'c', 'y' -> 'd', 'b' -> 'e', 'o' -> 'f').
16+
*
17+
* Return the decoded message.
18+
*/
19+
20+
/**
21+
* @param {string} key
22+
* @param {string} message
23+
* @return {string}
24+
*/
25+
var decodeMessage = function(key, message) {
26+
const substitution = new Map();
27+
let alphabetIndex = 0;
28+
29+
for (const char of key) {
30+
if (char !== ' ' && !substitution.has(char)) {
31+
substitution.set(char, String.fromCharCode(97 + alphabetIndex++));
32+
}
33+
}
34+
35+
let result = '';
36+
for (const char of message) {
37+
result += char === ' ' ? ' ' : substitution.get(char);
38+
}
39+
40+
return result;
41+
};

0 commit comments

Comments
 (0)