Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,53 @@ func (this *StringIterator) HasNext() bool {
*/
```

#### TypeScript

```ts
class StringIterator {
private d: [string, number][] = [];
private p: number = 0;

constructor(compressedString: string) {
const n = compressedString.length;
let i = 0;
while (i < n) {
const c = compressedString[i];
let x = 0;
i++;
while (i < n && !isNaN(Number(compressedString[i]))) {
x = x * 10 + Number(compressedString[i]);
i++;
}
this.d.push([c, x]);
}
}

next(): string {
if (!this.hasNext()) {
return ' ';
}
const ans = this.d[this.p][0];
this.d[this.p][1]--;
if (this.d[this.p][1] === 0) {
this.p++;
}
return ans;
}

hasNext(): boolean {
return this.p < this.d.length && this.d[this.p][1] > 0;
}
}

/**
* Your StringIterator object will be instantiated and called as such:
* var obj = new StringIterator(compressedString)
* var param_1 = obj.next()
* var param_2 = obj.hasNext()
*/
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,13 @@ stringIterator.hasNext(); // return True

<!-- solution:start -->

### Solution 1
### Solution 1: Parsing and Storing

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.

Then perform operations in `next` and `hasNext`.

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`.

<!-- tabs:start -->

Expand Down Expand Up @@ -260,6 +266,53 @@ func (this *StringIterator) HasNext() bool {
*/
```

#### TypeScript

```ts
class StringIterator {
private d: [string, number][] = [];
private p: number = 0;

constructor(compressedString: string) {
const n = compressedString.length;
let i = 0;
while (i < n) {
const c = compressedString[i];
let x = 0;
i++;
while (i < n && !isNaN(Number(compressedString[i]))) {
x = x * 10 + Number(compressedString[i]);
i++;
}
this.d.push([c, x]);
}
}

next(): string {
if (!this.hasNext()) {
return ' ';
}
const ans = this.d[this.p][0];
this.d[this.p][1]--;
if (this.d[this.p][1] === 0) {
this.p++;
}
return ans;
}

hasNext(): boolean {
return this.p < this.d.length && this.d[this.p][1] > 0;
}
}

/**
* Your StringIterator object will be instantiated and called as such:
* var obj = new StringIterator(compressedString)
* var param_1 = obj.next()
* var param_2 = obj.hasNext()
*/
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
class StringIterator {
private d: [string, number][] = [];
private p: number = 0;

constructor(compressedString: string) {
const n = compressedString.length;
let i = 0;
while (i < n) {
const c = compressedString[i];
let x = 0;
i++;
while (i < n && !isNaN(Number(compressedString[i]))) {
x = x * 10 + Number(compressedString[i]);
i++;
}
this.d.push([c, x]);
}
}

next(): string {
if (!this.hasNext()) {
return ' ';
}
const ans = this.d[this.p][0];
this.d[this.p][1]--;
if (this.d[this.p][1] === 0) {
this.p++;
}
return ans;
}

hasNext(): boolean {
return this.p < this.d.length && this.d[this.p][1] > 0;
}
}

/**
* Your StringIterator object will be instantiated and called as such:
* var obj = new StringIterator(compressedString)
* var param_1 = obj.next()
* var param_2 = obj.hasNext()
*/