Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public String decodeString(String s) {
- Stack<Integer> countStack = new Stack<>();
- Stack<String> valueStack = new Stack<>();
- StringBuilder sb = new StringBuilder();
- int idx = 0;
- String res = "";
- while(idx<s.length()){
- if(Character.isDigit(s.charAt(idx))){
- int cnt = 0;
- while(idx<s.length() && Character.isDigit(s.charAt(idx))){
- cnt = cnt*10+(s.charAt(idx)-'0');
- idx++;
- }
- countStack.add(cnt);
- }else if(s.charAt(idx)=='['){
- valueStack.add(res);
- res="";
- idx++;
- }else if(s.charAt(idx)==']'){
- StringBuilder tmp = new StringBuilder(valueStack.pop());
- int repeat = countStack.pop();
- for(int i=0;i<repeat;++i)
- tmp.append(res);
- res = tmp.toString();
- idx++;
- }else
- res+=s.charAt(idx++);
- }
- return res;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement