-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLc1286.java
47 lines (41 loc) · 1021 Bytes
/
Lc1286.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package leetcode;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
/**
* @author Kuma
* @date 2021年3月11日
* 1286. 字母组合迭代器
*/
public class Lc1286 {
}
class CombinationIterator {
Queue<String> res = new LinkedList<>();
StringBuilder tmp = new StringBuilder();
char[] ch;
int n;
public CombinationIterator(String characters, int combinationLength) {
ch = characters.toCharArray();
n = combinationLength;
search(0);
}
public void search(int i){
if (tmp.length() == n){
res.offer(tmp.toString());
return;
}
for (int j = i ; j < ch.length; j++) {
tmp.append(ch[j]);
if (!res.contains(tmp.toString())) {
search(j + 1);
}
tmp.deleteCharAt(tmp.length() - 1);
}
}
public String next() {
return res.poll();
}
public boolean hasNext() {
return !res.isEmpty();
}
}