/
solution.ts
31 lines (29 loc) · 925 Bytes
/
solution.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
// #Medium #Top_100_Liked_Questions #String #Stack #Recursion #Level_1_Day_14_Stack #Udemy_Strings
// #Big_O_Time_O(n)_Space_O(n) #2023_10_04_Time_51_ms_(81.02%)_Space_43_MB_(55.52%)
function decodeString(s: string): string {
let stack: string[] = []
for (const l of s) {
if (l !== ']') {
stack.push(l)
continue
}
parse()
}
function parse(): void {
let word: string = ''
while (stack[stack.length - 1] !== '[' && stack.length !== 0) {
word = stack[stack.length - 1] + word
stack.pop()
}
stack.pop()
const vov = '0123456789'
let counter = stack.pop()
while (vov.includes(stack[stack.length - 1])) {
counter = stack.pop() + counter
}
word = word.repeat(Number(counter))
stack.push(word)
}
return stack.join('')
}
export { decodeString }