LeetCode394 Decode String

it’s a very easily understanding problem, and you can get it just by looking at the example:
Input: s = “3[a2[c]]de”
Output: “accaccaccde”

and I know, such bracket related problem can be solved using stack, but how to implement it?

the code is as follows: (may be it’s better for us to use for each loop instead of while (i) )

class Solution {
    public String decodeString(String s) {
        if (s == null || s.length() == 0) {
            return "";
        }
        
        Stack<Integer> countStack = new Stack<>();
        Stack<StringBuilder> resStack = new Stack<>();
        
        int i = 0;
        int k = 0;
        StringBuilder cur = new StringBuilder(); //cur is res
        while (i < s.length()) {
            if (Character.isDigit(s.charAt(i))) {
                k = k * 10 + s.charAt(i) - '0';
            } else if (s.charAt(i) == '[') {
                countStack.push(k);//we know that when we met '[', k is alright
                resStack.push(cur);
                cur = new StringBuilder();
                k = 0; //and we can reinitialize k to 0
            } else if (s.charAt(i) == ']') {
                StringBuilder tmp = cur;//cur is the current string we wrapped out
                cur = resStack.pop(); //now cur is the string in resStack.pop() which is everything we build before
                k = countStack.pop();
                while (k > 0) {
                    //k = countStack.pop(); //why we can't simply use k? because we need to retive the closest k, and we also need to store previous k
                    cur.append(tmp);
                    k--;
                }
            } else { //if just regular letters
                cur.append(s.charAt(i));
            }
            i++;
        }
        
        return cur.toString();
    }
}

from above codes, you can see that stack is heavily used in such problems(brackets)
if one stack is not enough, then using two. and keep you mind not in linear.
for example: it is clear that we need to store numbers and strings. so we need to use stack to store them, and due to we need to construct the numbers and strings, so we need to use some variables to culmulaive them.
and when we meet ‘[’, then it means what? don’t just think we need to get what we are about to get, but that’s something we can’t control. so instead of thinking about what will come later, we need to think about this: when we meet ‘[’ that means what? means the number is already wrapped, or the string is already wrapped, like situations in “2[abc]” or “2[a[bc]]”. so that means we need to shove k and cur into countStack and resStack, and reinitialize those two things.
the same thing happens when we meet ‘]’, that means the string before this can be wrapped. and there is a bracket we need to wrapped. so we need to pop from both countStack and resStack. and continue to construct cur.
when we need digits, we just culmultive k.
and when we meet letters, just append them to cur.

posted @ 2020-06-12 23:15  EvanMeetTheWorld  阅读(12)  评论(0)    收藏  举报