-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
/
Copy pathSolution.java
49 lines (43 loc) · 1.09 KB
/
Solution.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
48
49
class StringIterator {
private List<Node> d = new ArrayList<>();
private int p;
public StringIterator(String compressedString) {
int n = compressedString.length();
int i = 0;
while (i < n) {
char c = compressedString.charAt(i);
int x = 0;
while (++i < n && Character.isDigit(compressedString.charAt(i))) {
x = x * 10 + (compressedString.charAt(i) - '0');
}
d.add(new Node(c, x));
}
}
public char next() {
if (!hasNext()) {
return ' ';
}
char ans = d.get(p).c;
if (--d.get(p).x == 0) {
++p;
}
return ans;
}
public boolean hasNext() {
return p < d.size() && d.get(p).x > 0;
}
}
class Node {
char c;
int x;
Node(char c, int x) {
this.c = c;
this.x = x;
}
}
/**
* Your StringIterator object will be instantiated and called as such:
* StringIterator obj = new StringIterator(compressedString);
* char param_1 = obj.next();
* boolean param_2 = obj.hasNext();
*/