Monday, August 17, 2020

LeetCode [604] Design Compressed String Iterator

 604. Design Compressed String Iterator

Easy

Design and implement a data structure for a compressed string iterator. The given compressed string will be in the form of each letter followed by a positive integer representing the number of this letter existing in the original uncompressed string.

Implement the StringIterator class:

  • next() Returns the next character if the original string still has uncompressed characters, otherwise returns a white space.
  • hasNext() Returns true if there is any letter needs to be uncompressed in the original string, otherwise returns false.

 

Example 1:

Input
["StringIterator", "next", "next", "next", "next", "next", "next", "hasNext", "next", "hasNext"]
[["L1e2t1C1o1d1e1"], [], [], [], [], [], [], [], [], []]
Output
[null, "L", "e", "e", "t", "C", "o", true, "d", true]

Explanation
StringIterator stringIterator = new StringIterator("L1e2t1C1o1d1e1");
stringIterator.next(); // return "L"
stringIterator.next(); // return "e"
stringIterator.next(); // return "e"
stringIterator.next(); // return "t"
stringIterator.next(); // return "C"
stringIterator.next(); // return "o"
stringIterator.hasNext(); // return True
stringIterator.next(); // return "d"
stringIterator.hasNext(); // return True

 

Constraints:

  • 1 <= compressedString.length <= 1000
  • compressedString consists of lower-case an upper-case English letters and digits.
  • The number of a single character repetitions in compressedString is in the range [1, 10^9]
  • At most 100 calls will be made to next and hasNext.
class StringIterator {
    char ch;//current char
    int count, index, n;
    char[] chars;
    public StringIterator(String compressedString) {
        chars = compressedString.toCharArray();
        count = 0;
        index = 0;
        n = chars.length;
    }
    
    public char next() {
        if(count>0){
            count--;
            return ch;
        }else if(index<n){
            ch = chars[index++];
            while(index<n && Character.isDigit(chars[index])){
                count = count*10 + (chars[index]-'0');
                index++;
            }
            count--;
            return ch;
        }
        return ' ';
    }
    
    public boolean hasNext() {
        return(index<n || count>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();
 */

No comments:

Post a Comment