Concise Java Solution with stack


  • 0
     public String decodeString(String s) {
            if (s == null || s.length() == 0)   return s;
            
            StringBuilder sb = new StringBuilder();
            Deque<Integer> cntStack = new ArrayDeque<>();
            Deque<String> resStack = new ArrayDeque<>();
            String res = "";
            int num = 0;
            for (char c : s.toCharArray()) {
                if (Character.isDigit(c)) {
                    num = 10*num + c - '0';
                }
                if (Character.isLetter(c)) {
                    res += c;
                }
                if (c == '[') {
                    cntStack.push(num);
                    resStack.push(res);
                    num = 0;
                    res = "";
                }
                if (c == ']') {
                    StringBuilder tmp = new StringBuilder();
                    int counts = cntStack.pop();
                    for (int i = 0; i < counts; ++i) {
                        tmp.append(res);
                    }
                    
                    res = resStack.pop() + tmp.toString();
                    num = 0;
                }
            }
            
            return res;
        }
    

Log in to reply
 

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