Iterator for Combination LeetCode solution in JavaScript

1
var CombinationIterator = function (characters, combinationLength) {
2
this.stack = Array.from({ length: combinationLength }, (_, i) => i);
3
this.combinationLength = combinationLength;
4
this.characters = characters;
5
};
6

7
CombinationIterator.prototype.next = function () {
8
const word = this.stack.map((i) => this.characters[i]).join("");
9

10
for (
11
let lastIndex = this.characters.length - 1;
12
this.stack.at(-1) == lastIndex;
13

14
)
15
lastIndex = this.stack.pop() - 1;
16

17
if (this.stack.length > 0)
18
for (
19
let i = this.stack.pop() + 1;
20
this.stack.length < this.combinationLength;
21
i++
22
)
23
this.stack.push(i);
24

25
return word;
26
};
27

28
CombinationIterator.prototype.hasNext = function () {
29
return this.stack.length > 0;
30
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0