-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCompressedStringIterator.java
More file actions
45 lines (40 loc) · 1.17 KB
/
CompressedStringIterator.java
File metadata and controls
45 lines (40 loc) · 1.17 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
public class StringIterator {
private final String str;
private char curChar;
private int curCharCount;
private int nextId;
public StringIterator(String compressedString) {
this.str = compressedString;
nextId = 0;
moveToNextAvailable();
}
private void moveToNextAvailable() {
for (; curCharCount == 0 && nextId < str.length();) {
curChar = str.charAt(nextId++);
for(; nextId < str.length() && Character.isDigit(str.charAt(nextId)); nextId++) {
int d = str.charAt(nextId) - '0';
curCharCount = 10 * curCharCount + d;
}
}
}
public char next() {
if (!hasNext()) {
return ' ';
}
char ret = curChar;
curCharCount--;
if (curCharCount == 0) {
moveToNextAvailable();
}
return ret;
}
public boolean hasNext() {
return curCharCount > 0;
}
}
/**
* 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();
*/