Simple Java Solution no additional stacks needed


  • 0
    K

    public class Solution {
    public String decodeString(String s) {
    if(s==null||s.length()==0)return new String();
    while(s.lastIndexOf("[")!=-1){
    int indexR = s.lastIndexOf("[");
    int indexL = indexR + s.substring(indexR+1).indexOf("]")+1;
    String inner = s.substring(indexR+1,indexL);
    StringBuffer x = new StringBuffer();
    StringBuffer number = new StringBuffer();
    int postion = indexR-1;
    while(postion>=0&&Character.isDigit(s.charAt(postion))){
    number.append(s.charAt(postion--));
    }
    int count = Integer.parseInt(number.reverse().toString());
    while(count>=1){
    x.append(inner);
    count--;
    }
    s = s.substring(0,postion+1)+x.toString()+s.substring(indexL+1);
    }
    return s;
    }
    }


Log in to reply
 

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