Java solution using one stack


  • 0
    B
    public String decodeString(String s) {
        String cur="";
        int num=0;
        Stack<String>stack=new Stack<String>();
        char ori[]=s.toCharArray();
        for(int i=0;i<s.length();i++){
            if(ori[i]>='a'&&ori[i]<='z'){
                cur+=ori[i];
            }
            if(ori[i]>='0'&&ori[i]<='9'){
                num=num*10+ori[i]-'0';
            }
            if(ori[i]=='['){
                stack.push(num+"");
                stack.push(cur);
                cur="";
                num=0;
            }
            if(ori[i]==']'){
                String temp=stack.pop();
                int size=Integer.parseInt(stack.pop());
                for(int j=0;j<size;j++){
                    temp+=cur;
                }
                 cur=temp;
                 num=0;
           }
        }
        
        return cur;
    }

Log in to reply
 

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