-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy path1286-iterator-for-combination.js
51 lines (46 loc) · 1.42 KB
/
1286-iterator-for-combination.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/**
* 1286. Iterator for Combination
* https://leetcode.com/problems/iterator-for-combination/
* Difficulty: Medium
*
* Design the CombinationIterator class:
* - CombinationIterator(string characters, int combinationLength) Initializes the object with a
* string characters of sorted distinct lowercase English letters and a number combinationLength
* as arguments.
* - next() Returns the next combination of length combinationLength in lexicographical order.
* - hasNext() Returns true if and only if there exists a next combination.
*/
/**
* @param {string} characters
* @param {number} combinationLength
*/
var CombinationIterator = function(characters, combinationLength) {
this.chars = characters;
this.length = combinationLength;
this.indices = new Array(combinationLength).fill(0).map((_, i) => i);
this.hasMore = true;
};
/**
* @return {string}
*/
CombinationIterator.prototype.next = function() {
const result = this.indices.map(i => this.chars[i]).join('');
this.hasMore = false;
for (let i = this.length - 1; i >= 0; i--) {
if (this.indices[i] < this.chars.length - (this.length - i)) {
this.indices[i]++;
for (let j = i + 1; j < this.length; j++) {
this.indices[j] = this.indices[j - 1] + 1;
}
this.hasMore = true;
break;
}
}
return result;
};
/**
* @return {boolean}
*/
CombinationIterator.prototype.hasNext = function() {
return this.hasMore;
};