package leetcode;

/**
 * https://leetcode.com/problems/design-compressed-string-iterator/
 */
public class Problem604 {
    public static class StringIterator {
        private final String compressedString;
        private int idx;
        private int count;
        private char ch;

        public StringIterator(String compressedString) {
            this.compressedString = compressedString;
        }

        public char next() {
            if (!hasNext()) {
                return  ' ';
            }
            if (count == 0) {
                ch = compressedString.charAt(idx);
                // get the number
                idx++;
                StringBuilder sb = new StringBuilder();
                char c = compressedString.charAt(idx);
                while (c >= '0' && c <= '9' && idx < compressedString.length()) {
                    sb.append(c);
                    idx++;
                    if (idx < compressedString.length()) {
                        c = compressedString.charAt(idx);
                    }
                }
                count = Integer.parseInt(sb.toString());
            }
            count--;
            return ch;
        }

        public boolean hasNext() {
            return !(count == 0 && idx >= compressedString.length());
        }
    }

    public static void main(String[] args) {
        StringIterator iter = new StringIterator("L1e2t1C1o1d1e1");
//        System.out.println(iter.next()); // L
//        System.out.println(iter.next()); // e
//        System.out.println(iter.next()); // e
//        System.out.println(iter.next()); // t
//        System.out.println(iter.next()); // C
//        System.out.println(iter.next()); // o
//        System.out.println(iter.next()); // d
//        System.out.println(iter.hasNext()); // true
//        System.out.println(iter.next()); // e
//        System.out.println(iter.hasNext()); // false
//        System.out.println(iter.next()); // ' '

        iter = new StringIterator("a10b11c1d5e12");
        while (iter.hasNext()) {
            System.out.print(iter.next());
        }
        System.out.println();
    }
}

@@ -1414,18 +1414,16 @@
 d1e1%22);%0A
-//
         
@@ -1460,18 +1460,16 @@
 ); // L%0A
-//
         
@@ -1498,34 +1498,32 @@
 r.next()); // e%0A
-//
         System.o
@@ -1544,34 +1544,32 @@
 r.next()); // e%0A
-//
         System.o
@@ -1598,18 +1598,16 @@
 ); // t%0A
-//
         
@@ -1644,18 +1644,16 @@
 ); // C%0A
-//
         
@@ -1690,18 +1690,16 @@
 ); // o%0A
-//
         
@@ -1736,18 +1736,16 @@
 ); // d%0A
-//
         
@@ -1788,18 +1788,16 @@
 // true%0A
-//
         
@@ -1834,18 +1834,16 @@
 ); // e%0A
-//
         
@@ -1887,18 +1887,16 @@
 / false%0A
-//
         

Update problem 604