forked from codemistic/Data-Structures-and-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetcode-IteratorforCombination.java
More file actions
42 lines (37 loc) · 1.19 KB
/
Leetcode-IteratorforCombination.java
File metadata and controls
42 lines (37 loc) · 1.19 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
/**Leetcode - 1286 - Iterator for Combination (https://leetcode.com/problems/iterator-for-combination/)
Question:
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.
*/
class CombinationIterator {
String s;
int l;
int c=0;
List<String> ar=new LinkedList<String>();
public CombinationIterator(String sr, int d) {
s=sr;l=d;
traversal("",d,0);
}
public String next() {
c++;
return ar.get(c-1);
}
public boolean hasNext() {
if(c<ar.size()){
return true;
}
return false;
}
public void traversal(String st,int d,int idx){
if(st.length()==d){
ar.add(st);
return;
}
for(int i=idx;i<s.length();i++){
traversal(st+s.charAt(i),d,i+1);
}
}
}
//Contribution by shubhrastogi07