deno.land / x / masx200_leetcode_test@10.6.5 / design-compressed-string-iterator / index.ts
1234567891011121314151617181920212223242526export default class StringIterator { #gen: Generator<string, void, unknown>; #result: IteratorResult<string, void>; constructor(compressedString: string) { this.#gen = RLEGenerator(compressedString); this.#result = this.#gen.next(); } next() { const value = this.#result.value; if (typeof value === "undefined") return ""; this.#result = this.#gen.next(); return value; } hasNext() { return !this.#result.done; }}function* RLEGenerator(encoding: string) { for (let i = 0; i < encoding.length; i += 2) { const count = Number(encoding[i + 1]); for (let j = 0; j < count; j++) { yield encoding[i]; } }}
Version Info