-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringIterator.java
More file actions
68 lines (59 loc) · 1.58 KB
/
StringIterator.java
File metadata and controls
68 lines (59 loc) · 1.58 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
56
57
58
59
60
61
62
63
64
65
66
67
68
import java.util.ArrayList;
import java.util.List;
public class StringIterator
{
String expandedString = "";
List<Integer> list = new ArrayList<>();
int pos = 0;
public StringIterator( String compressedString )
{
int i = 0, j = 0;
StringBuilder sb = new StringBuilder();
while ( i < compressedString.length() )
{
while ( j < compressedString.length() && !( compressedString.charAt( j ) <= '9' && compressedString.charAt( j ) >= '0' ) )
j++;
String s = compressedString.substring( i, j );
expandedString += s;
i = j;
while ( j < compressedString.length() && compressedString.charAt( j ) <= '9' && compressedString.charAt( j ) >= '0' )
j++;
int val = Integer.valueOf( compressedString.substring( i, j ) );
i = j;
list.add( val );
}
}
public char next()
{
if ( hasNext() )
{
list.set( pos, list.get( pos ) - 1 );
char c = expandedString.charAt( pos );
if ( list.get( pos ) <= 0 )
pos++;
return c;
}
else
return ' ';
}
public boolean hasNext()
{
if ( pos < list.size() && list.get( pos ) > 0 )
return true;
return false;
}
public static void main( String[] args )
{
StringIterator s = new StringIterator( "L1e2t1C1o1d1e1" );
String[] op = { "StringIterator", "next", "next", "next", "next", "next", "next", "hasNext", "next", "hasNext" };
// {{"a1234567890b1234567890"},{},{},{},{},{},{},{},{},{}};
for ( int i = 1; i < op.length; i++ )
{
if ( op[i].equals( "next" ) )
System.out.println( s.next() );
else
System.out.println( s.hasNext() );
}
// {{"L1e2t1C1o1d1e1"},{},{},{},{},{},{},{},{},{}};
}
}