Simple Java solution for reference


  • 0
    R
    public class StringIterator {
        
        private char curr;
        private int count;
        private int index;
        private String str;
    
        public StringIterator(String compressedString) {
            str = compressedString;
            reset();
        }
        
        private void reset() {
            curr = ' ';
            count = 0;
            index = 0;
        }
        
        private void read() {
            curr = str.charAt(index++);
            int num = 0;
            while(index < str.length() && Character.isDigit(str.charAt(index))) {
                num = num * 10 + (str.charAt(index++) - '0');
            }
            count = num;
        }
        
        public char next() {
            if(count == 0 && index == str.length()) {
                return ' ';
            } else {
                if(count == 0) read();
                count--;
                return curr;
            }
        }
        
        public boolean hasNext() {
            return count > 0 || index < str.length();
        }
    }
    
    
    

Log in to reply
 

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