Iterator for Combination LeetCode solution in Java

1
class CombinationIterator {
2

3
private Queue<String> allCombinations;
4

5
public CombinationIterator(String characters, int combinationLength) {
6
this.allCombinations = new LinkedList<>();
7
generateAllCombinations(characters, 0, combinationLength, new StringBuilder());
8
}
9

10
private void generateAllCombinations(
11
String characters, int index, int combinationLength, StringBuilder currentString) {
12

13
if (currentString.length() == combinationLength) {
14
this.allCombinations.offer(currentString.toString());
15
return;
16
}
17

18
for (int i = index; i < characters.length(); i++) {
19
currentString.append(characters.charAt(i));
20
generateAllCombinations(characters, i + 1, combinationLength, currentString);
21
currentString.deleteCharAt(currentString.length() - 1);
22
}
23
}
24

25
public String next() {
26
return this.allCombinations.poll();
27
}
28

29
public boolean hasNext() {
30
return !this.allCombinations.isEmpty();
31
}
32
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0