My easy to understand Java solution, beats 92%


  • 0
    W
    class StringIterator {
        String compressed = null;
        int p = 0;
        char currentChar = ' ';
        int currentCount = 0;
        
        public StringIterator(String compressedString) {
            compressed = compressedString;
        }
        
        public char next() {
            if (!hasNext()) return ' ';
            
            if (currentCount == 0) {
                currentChar = compressed.charAt(p);
                while (++p < compressed.length()) {
                    char c = compressed.charAt(p);
                    if (c < '0' || c > '9') break;
                    
                    int d = c - '0';
                    currentCount = currentCount * 10 + d;
                } 
            }
            currentCount --;
            return currentChar;
        }
        
        public boolean hasNext() {
            return p < compressed.length() || currentCount > 0;    
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.