-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path26-394.ts
50 lines (38 loc) · 1.29 KB
/
26-394.ts
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
// https://leetcode.com/problems/decode-string/?envType=study-plan-v2&envId=leetcode-75
const decodeString = (str: string): string => {
// ===/ CASE OF: ""
if (!str) return "";
const openingBracketIdx = str.indexOf("[");
// ===/ CASE OF: "ef"
if (openingBracketIdx === -1) return str;
const firstDigitIdx = RegExp(/\d/).exec(str)?.index ?? -1;
const count = +str.substr(0, openingBracketIdx);
// ===/ CASE OF: "ef3[a]..."
if (isNaN(count)) {
const currStr = str.substr(0, firstDigitIdx);
const remainingStr = str.substr(firstDigitIdx);
return currStr + decodeString(remainingStr);
}
// ===/ CASE OF: "3[a]..."
let closingBracketIdx = -1;
for (
let i = openingBracketIdx, closingBracketsToSkipCount = 0;
i < str.length;
i++
) {
if (str[i] === "[") closingBracketsToSkipCount++;
else if (str[i] === "]") {
closingBracketsToSkipCount--;
if (!closingBracketsToSkipCount) {
closingBracketIdx = i;
break;
}
}
}
const nestedStr = str.substring(openingBracketIdx + 1, closingBracketIdx);
const remainingStr = str.substr(closingBracketIdx + 1);
const decodedStr = decodeString(nestedStr);
let resStr = "";
for (let i = 0; i < count; i++) resStr += decodedStr;
return resStr + decodeString(remainingStr);
};