-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleetCode_1286.java
More file actions
44 lines (37 loc) · 1.12 KB
/
Copy pathleetCode_1286.java
File metadata and controls
44 lines (37 loc) · 1.12 KB
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
class CombinationIterator {
List<String> result;
String str;
int k;
int itr;
public CombinationIterator(String characters, int combinationLength) {
str = characters;
k = combinationLength;
itr=0;
result = new ArrayList<>();
backTrack("", str, k);
}
public String next() {
return result.get(itr++);
}
public boolean hasNext() {
return itr < result.size();
}
void backTrack(String current, String s, int k){
if(current.length() == k){
result.add(current);
return;
}
for(int i=0; i<s.length(); i++){
current = current.concat(s.substring(i, i+1));
String ss = s.substring(i+1);
backTrack(current, ss, k);
current = current.substring(0, current.length()-1);
}
}
}
/**
* Your CombinationIterator object will be instantiated and called as such:
* CombinationIterator obj = new CombinationIterator(characters, combinationLength);
* String param_1 = obj.next();
* boolean param_2 = obj.hasNext();
*/