diff --git a/solution/0600-0699/0604.Design Compressed String Iterator/README.md b/solution/0600-0699/0604.Design Compressed String Iterator/README.md index 9d8866ead6b51..49ed7490b6fa8 100644 --- a/solution/0600-0699/0604.Design Compressed String Iterator/README.md +++ b/solution/0600-0699/0604.Design Compressed String Iterator/README.md @@ -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() + */ +``` + diff --git a/solution/0600-0699/0604.Design Compressed String Iterator/README_EN.md b/solution/0600-0699/0604.Design Compressed String Iterator/README_EN.md index cc5512a0be612..b5c23f018d70e 100644 --- a/solution/0600-0699/0604.Design Compressed String Iterator/README_EN.md +++ b/solution/0600-0699/0604.Design Compressed String Iterator/README_EN.md @@ -67,7 +67,13 @@ stringIterator.hasNext(); // return True -### 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`. @@ -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() + */ +``` + diff --git a/solution/0600-0699/0604.Design Compressed String Iterator/Solution.ts b/solution/0600-0699/0604.Design Compressed String Iterator/Solution.ts new file mode 100644 index 0000000000000..6313052a68374 --- /dev/null +++ b/solution/0600-0699/0604.Design Compressed String Iterator/Solution.ts @@ -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() + */