-
Notifications
You must be signed in to change notification settings - Fork 0
/
decode-string.js
42 lines (31 loc) · 1.33 KB
/
decode-string.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
/**
* @param {string} s
* @return {string}
*/
var decodeString = function(s) {
let numStack = []; // Store a stack of multiples of num
let strStack = []; // Store the stack of str to be spliced
let num = 0; // Multiple "porters"
let res = ''; // The "porter" of the string
for (const char of s) {
// Character by character scan
if (!isNaN(char)) {
// Encounter numbers
num = num * 10 + Number(char); // Read number
} else if (char === '[') {
// Encountered [
strStack.push(res); // The result string enters the strStack stack and waits
res = ''; // Cleared after finishing stacking
numStack.push(num); // The multiple num enters the stack and waits
num = 0; // Cleared after finishing stacking
} else if (char === ']') {
// Encountered ], the top of the two stacks is popped
let repeatTimes = numStack.pop(); // Get the number of copies
res = strStack.pop() + res.repeat(repeatTimes); // Construct substring
} else {
// When a letter is encountered, append it to the result string
res += char;
}
}
return res;
};