forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDecode String.js
More file actions
28 lines (26 loc) · 731 Bytes
/
Decode String.js
File metadata and controls
28 lines (26 loc) · 731 Bytes
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
// Runtime: 84 ms (Top 55.68%) | Memory: 41.5 MB (Top 92.94%)
var decodeString = function(s) {
const stack = [];
for (let char of s) {
if (char === "]") {
let curr = stack.pop();
let str = '';
while (curr !== '[') {
str = curr+ str;
curr = stack.pop();
}
let num = "";
curr = stack.pop();
while (!isNaN(curr)) {
num = curr + num;
curr = stack.pop();
}
stack.push(curr);
for (let i=0; i<Number(num);i++) {
stack.push(str);
}
}
else stack.push(char);
}
return stack.join('');
};