Given an encoded string, return its decoded string.
The encoding rule is: k[encoded_string], where the encoded_string inside the square brackets is being repeated exactly k times. Note that k is guaranteed to be a positive integer.
You may assume that the input string is always valid; No extra white spaces, square brackets are well-formed, etc.
Furthermore, you may assume that the original data does not contain any digits and that digits are only for those repeat numbers, k. For example, there won't be input like 3a or 2[4].
Input: s = "3[a]2[bc]"
Output: "aaabcbc"
Input: s = "3[a2[c]]"
Output: "accaccacc"
Input: s = "2[abc]3[cd]ef"
Output: "abcabccdcdcdef"
Input: s = "abc3[cd]xyz"
Output: "abccdcdcdxyz"
1 <= s.length <= 30s consists of lowercase English letters, digits, and square brackets '[]'.s is guaranteed to be a valid input.s are in the range [1, 300].숫자 스택과 문자 스택을 만든다.
문자열의 한 문자씩 본다.
현재 문자를 curLetter에 저장해둔다.
curLetter가 문자인지, 숫자인지, 괄호인지 보고 각각의 문장을 수행한다.
간략하게,
숫자가 나오면 -> 여는 괄호가 나올 때까지 모아둔다.
문자가 나오면 -> 현재 문자를 모은다.
여는 괄호가 나오면 -> 모은 숫자와 문자를 각 스택에 넣는다. 그리고 모으던 스트링 빌더들을 초기화한다.
닫는 괄호가 나오면 -> 문자 스택에서 꺼내와 임시 스트링빌더에 넣고, 숫자 스택에서 꺼낸 수만큼 저장된 문자를 반복하여 임시 스트링빌더에 넣는다.
다 넣으면 결과에 임시 스트링빌더를 넣는다.
결과 스트링빌더를 문자로 바꾸어 반환한다.
class Solution {
public String decodeString(String s) {
Stack<String> strStack = new Stack<>();
Stack<Integer> numStack = new Stack<>();
StringBuilder res = new StringBuilder();
StringBuilder numSb = new StringBuilder();
int idx = 0;
while (idx < s.length()) {
char curLetter = s.charAt(idx);
if (Character.isDigit(curLetter)) {
numSb.append(curLetter);
idx++;
} else if (curLetter == '[') {
numStack.push(Integer.parseInt(numSb.toString()));
strStack.push(res.toString());
numSb = new StringBuilder();
res = new StringBuilder();
idx++;
} else if (curLetter == ']') {
StringBuilder temp = new StringBuilder(strStack.pop());
int repeatTimes = numStack.pop();
for (int i = 0; i < repeatTimes; i++) {
temp.append(res);
}
res = temp;
idx++;
} else {
res.append(s.charAt(idx++));
}
}
return res.toString();
}
}