-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1286.iterator-for-combination.java
More file actions
55 lines (47 loc) · 1.87 KB
/
Copy path1286.iterator-for-combination.java
File metadata and controls
55 lines (47 loc) · 1.87 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
45
46
47
48
49
50
51
52
53
54
55
class CombinationIterator {
Stack<Character> st; // stack to store the characters leading to the creation of a single combination
Map<Character, Integer> ch2Idx; // map to store character to index
String result, str; // str - same as characters. result -- the result string representing a combination
int combLength; // same as combinationLength
public CombinationIterator(String characters, int combinationLength) {
combLength = combinationLength;
ch2Idx = new HashMap();
str = characters;
st = new Stack();
result = "";
// create the first combination
for (Character ch : characters.toCharArray()) {
st.push(ch);
result = result + ch;
if (st.size()==combinationLength) break;
}
int idx = 0;
// set up the mapping between character --> index
for (Character ch : characters.toCharArray()) {
ch2Idx.put(ch, idx++);
}
}
public String next() {
String currResult = result;
// process the next result
int idx = str.length()-1;
// keep on removing the last character from the stack/result till the position where idx can be moved ahead
while (!st.isEmpty() && ch2Idx.get(st.peek())==idx) {
st.pop();
idx--;
result = result.substring(0, result.length()-1);
}
if (st.isEmpty()) return currResult; // there is no next result to pre-process
idx = ch2Idx.get(st.pop()); // we need to add elements after this idx
result = result.substring(0, result.length()-1);
while (st.size()!=combLength) {
Character temp = str.charAt(++idx);
result+=temp;
st.push(temp);
}
return currResult;
}
public boolean hasNext() {
return !st.isEmpty();
}
}