Skip to content

Commit fda9fec

Browse files
authored
feat: add solutions to lc problem: No.0604 (#4326)
No.604.Design Compressed String Iterator
1 parent 17849a8 commit fda9fec

File tree

3 files changed

+143
-1
lines changed

3 files changed

+143
-1
lines changed

solution/0600-0699/0604.Design Compressed String Iterator/README.md

+47
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,53 @@ func (this *StringIterator) HasNext() bool {
267267
*/
268268
```
269269

270+
#### TypeScript
271+
272+
```ts
273+
class StringIterator {
274+
private d: [string, number][] = [];
275+
private p: number = 0;
276+
277+
constructor(compressedString: string) {
278+
const n = compressedString.length;
279+
let i = 0;
280+
while (i < n) {
281+
const c = compressedString[i];
282+
let x = 0;
283+
i++;
284+
while (i < n && !isNaN(Number(compressedString[i]))) {
285+
x = x * 10 + Number(compressedString[i]);
286+
i++;
287+
}
288+
this.d.push([c, x]);
289+
}
290+
}
291+
292+
next(): string {
293+
if (!this.hasNext()) {
294+
return ' ';
295+
}
296+
const ans = this.d[this.p][0];
297+
this.d[this.p][1]--;
298+
if (this.d[this.p][1] === 0) {
299+
this.p++;
300+
}
301+
return ans;
302+
}
303+
304+
hasNext(): boolean {
305+
return this.p < this.d.length && this.d[this.p][1] > 0;
306+
}
307+
}
308+
309+
/**
310+
* Your StringIterator object will be instantiated and called as such:
311+
* var obj = new StringIterator(compressedString)
312+
* var param_1 = obj.next()
313+
* var param_2 = obj.hasNext()
314+
*/
315+
```
316+
270317
<!-- tabs:end -->
271318

272319
<!-- solution:end -->

solution/0600-0699/0604.Design Compressed String Iterator/README_EN.md

+54-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,13 @@ stringIterator.hasNext(); // return True
6767

6868
<!-- solution:start -->
6969

70-
### Solution 1
70+
### Solution 1: Parsing and Storing
71+
72+
Parse the `compressedString` into characters $c$ and their corresponding repetition counts $x$, and store them in an array or list $d$. Use $p$ to point to the current character.
73+
74+
Then perform operations in `next` and `hasNext`.
75+
76+
The initialization time complexity is $O(n)$, and the time complexity of the other operations is $O(1)$. Here, $n$ is the length of `compressedString`.
7177

7278
<!-- tabs:start -->
7379

@@ -260,6 +266,53 @@ func (this *StringIterator) HasNext() bool {
260266
*/
261267
```
262268

269+
#### TypeScript
270+
271+
```ts
272+
class StringIterator {
273+
private d: [string, number][] = [];
274+
private p: number = 0;
275+
276+
constructor(compressedString: string) {
277+
const n = compressedString.length;
278+
let i = 0;
279+
while (i < n) {
280+
const c = compressedString[i];
281+
let x = 0;
282+
i++;
283+
while (i < n && !isNaN(Number(compressedString[i]))) {
284+
x = x * 10 + Number(compressedString[i]);
285+
i++;
286+
}
287+
this.d.push([c, x]);
288+
}
289+
}
290+
291+
next(): string {
292+
if (!this.hasNext()) {
293+
return ' ';
294+
}
295+
const ans = this.d[this.p][0];
296+
this.d[this.p][1]--;
297+
if (this.d[this.p][1] === 0) {
298+
this.p++;
299+
}
300+
return ans;
301+
}
302+
303+
hasNext(): boolean {
304+
return this.p < this.d.length && this.d[this.p][1] > 0;
305+
}
306+
}
307+
308+
/**
309+
* Your StringIterator object will be instantiated and called as such:
310+
* var obj = new StringIterator(compressedString)
311+
* var param_1 = obj.next()
312+
* var param_2 = obj.hasNext()
313+
*/
314+
```
315+
263316
<!-- tabs:end -->
264317

265318
<!-- solution:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
class StringIterator {
2+
private d: [string, number][] = [];
3+
private p: number = 0;
4+
5+
constructor(compressedString: string) {
6+
const n = compressedString.length;
7+
let i = 0;
8+
while (i < n) {
9+
const c = compressedString[i];
10+
let x = 0;
11+
i++;
12+
while (i < n && !isNaN(Number(compressedString[i]))) {
13+
x = x * 10 + Number(compressedString[i]);
14+
i++;
15+
}
16+
this.d.push([c, x]);
17+
}
18+
}
19+
20+
next(): string {
21+
if (!this.hasNext()) {
22+
return ' ';
23+
}
24+
const ans = this.d[this.p][0];
25+
this.d[this.p][1]--;
26+
if (this.d[this.p][1] === 0) {
27+
this.p++;
28+
}
29+
return ans;
30+
}
31+
32+
hasNext(): boolean {
33+
return this.p < this.d.length && this.d[this.p][1] > 0;
34+
}
35+
}
36+
37+
/**
38+
* Your StringIterator object will be instantiated and called as such:
39+
* var obj = new StringIterator(compressedString)
40+
* var param_1 = obj.next()
41+
* var param_2 = obj.hasNext()
42+
*/

0 commit comments

Comments
 (0)