Java clean and fast iterative solution - no stack!


  • 0
    R
    public class Solution {
        public String decodeString(String s) {
            while(s.indexOf("]")!=-1){
                s = decodeFirst(s);
            }
            return s;
        }
       
        String decodeFirst(String s){
            int close = s.indexOf("]");
            int i = close-1;
            int open = -1;
            while(--i>0 && open==-1){
                if(s.charAt(i)=='['){
                    open = i;
                    break;
                }
            }
            int numStart = -1;
            while(--i>=0){
                if(s.charAt(i)<'0' || s.charAt(i)>'9')
                    break;
            }
            numStart = i+1;
            int k = Integer.parseInt(s.substring(numStart, open));
            String sub = s.substring(open+1, close);
            StringBuilder sbSub = new StringBuilder();
            for(int j = 0; j<k; j++){
                sbSub.append(sub);
            }
            StringBuilder sb = new StringBuilder();
            sb.append(s.substring(0, numStart));
            sb.append(sbSub.toString());
            sb.append(s.substring(close+1, s.length()));
            return sb.toString();
        }
    }
    

Log in to reply
 

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