-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIteratorforCombination.java
More file actions
31 lines (29 loc) · 824 Bytes
/
IteratorforCombination.java
File metadata and controls
31 lines (29 loc) · 824 Bytes
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
import java.util.*;
import java.util.lang.*;
import java.io.*;
class CombinationIterator {
private String s;
Queue<String>q;
private void getcombination(int start,int length, StringBuilder txt){
if(length==0){
q.add(txt.toString());
return ;
}
for(int i=start; i<=s.length()-length; ++i){
txt.append(s.charAt(i));
getcombination(i+1,length-1,txt);
txt.deleteCharAt(txt.length()-1);
}
}
public CombinationIterator(String characters, int combinationLength) {
s=characters;
q=new LinkedList();
getcombination(0,combinationLength,new StringBuilder());
}
public String next() {
return q.poll();
}
public boolean hasNext() {
return !q.isEmpty();
}
}