-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathIterator for Combination.java
31 lines (24 loc) · 1.03 KB
/
Iterator for Combination.java
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
class CombinationIterator {
private Queue<String> allCombinations;
public CombinationIterator(String characters, int combinationLength) {
this.allCombinations = new LinkedList<>();
generateAllCombinations(characters,0,combinationLength,new StringBuilder());
}
private void generateAllCombinations(String characters,int index,int combinationLength,StringBuilder currentString){
if(currentString.length() == combinationLength){
this.allCombinations.offer(currentString.toString());
return;
}
for(int i = index ; i < characters.length() ; i++){
currentString.append(characters.charAt(i));
generateAllCombinations(characters,i+1,combinationLength,currentString);
currentString.deleteCharAt(currentString.length()-1);
}
}
public String next() {
return this.allCombinations.poll();
}
public boolean hasNext() {
return !this.allCombinations.isEmpty();
}
}