Skip to content

Commit 6417623

Browse files
committed
feat: add typescript solution to lc problem: No.0394
No.0394.Decode String
1 parent 00bc504 commit 6417623

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

solution/0300-0399/0394.Decode String/README.md

+27
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,33 @@ class Solution {
114114
}
115115
```
116116

117+
### **TypeScript**
118+
119+
```ts
120+
function decodeString(s: string): string {
121+
let ans = '';
122+
let stack = [];
123+
let count = 0; // repeatCount
124+
for (let cur of s) {
125+
if ((/[0-9]/.test(cur))) {
126+
count = count * 10 + Number(cur);
127+
} else if (/[a-z]/.test(cur)) {
128+
ans += cur;
129+
} else if ('[' == cur) {
130+
stack.push([ans, count]);
131+
// reset
132+
ans = '';
133+
count = 0;
134+
} else {
135+
// match ']'
136+
let [pre, count] = stack.pop();
137+
ans = pre + ans.repeat(count);
138+
}
139+
}
140+
return ans;
141+
};
142+
```
143+
117144
### **...**
118145

119146
```

solution/0300-0399/0394.Decode String/README_EN.md

+27
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,33 @@ class Solution {
9494
}
9595
```
9696

97+
### **TypeScript**
98+
99+
```ts
100+
function decodeString(s: string): string {
101+
let ans = '';
102+
let stack = [];
103+
let count = 0; // repeatCount
104+
for (let cur of s) {
105+
if ((/[0-9]/.test(cur))) {
106+
count = count * 10 + Number(cur);
107+
} else if (/[a-z]/.test(cur)) {
108+
ans += cur;
109+
} else if ('[' == cur) {
110+
stack.push([ans, count]);
111+
// reset
112+
ans = '';
113+
count = 0;
114+
} else {
115+
// match ']'
116+
let [pre, count] = stack.pop();
117+
ans = pre + ans.repeat(count);
118+
}
119+
}
120+
return ans;
121+
};
122+
```
123+
97124
### **...**
98125

99126
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
function decodeString(s: string): string {
2+
let ans = '';
3+
let stack = [];
4+
let count = 0; // repeatCount
5+
for (let cur of s) {
6+
if ((/[0-9]/.test(cur))) {
7+
count = count * 10 + Number(cur);
8+
} else if (/[a-z]/.test(cur)) {
9+
ans += cur;
10+
} else if ('[' == cur) {
11+
stack.push([ans, count]);
12+
// reset
13+
ans = '';
14+
count = 0;
15+
} else {
16+
// match ']'
17+
let [pre, count] = stack.pop();
18+
ans = pre + ans.repeat(count);
19+
}
20+
}
21+
return ans;
22+
};

0 commit comments

Comments
 (0)