Java Solution using Wrapper Class and Stack


  • 0
    public class Solution {
        public String decodeString(String s) {
            char[] sArray = s.toCharArray();
            Stack<StringNum> stack = new Stack<>();
            StringNum curr = new StringNum();
            int num = 0;
    
            for(int i = 0; i < sArray.length; i++) {
                if(Character.isDigit(sArray[i])) {
                    int start = i;
                    while(i + 1 < sArray.length && Character.isDigit(sArray[i + 1])) i++;
                    curr.num = Integer.parseInt(s.substring(start, i + 1));
                } else if(Character.isLetter(sArray[i])) {
                    int start = i;
                    while(i + 1 < sArray.length && Character.isLetter(sArray[i + 1])) i++;
                    curr.word.append(s.substring(start, i + 1));
                } else if(sArray[i] == '[') {
                    stack.push(curr);
                    curr = new StringNum();
                } else if(sArray[i] == ']') {
                    String post = curr.word.toString();
                    curr = stack.pop();
                    for(int j = 0; j < curr.num; j++) curr.word.append(post);
                }
            }
            return curr.word.toString();
        }
    
        class StringNum {
            public StringBuilder word = new StringBuilder();
            int num = 0;
        }
    }
    

  • 0
    T

    Tip for non-LeetCode: Careful with isDigit: "١٩" (Arabic 19) is accepted by it (per docs), but parseInt will probably blow up.


  • 0

    @TWiStErRob Thank you for your tip, I think the safe way is to use c >= '0' && c <= '9'.


Log in to reply
 

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