-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDecodeString.java
More file actions
48 lines (41 loc) · 1.48 KB
/
DecodeString.java
File metadata and controls
48 lines (41 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
public class DecodeString {
public static String decodeString(String s) {
while (s.contains("[")) {
StringBuilder res = new StringBuilder();
int intIndex = 0;
int preOpenIndex = 0;
int times = 0;
char[] charArr = s.toCharArray();
for (int i = 0; i < charArr.length; i++) {
if (Character.isDigit(charArr[i])) {
if(preOpenIndex != 0) {
times = charArr[i] - '0';
intIndex = i;
} else {
if (times == 0)
intIndex = i;
times = 10 * times + (charArr[i] - '0');
}
}
if (charArr[i] == '[') {
preOpenIndex = i;
// Reset times
} else if (charArr[i] == ']') {
System.out.println(times);
System.out.println(intIndex);
for (int j = 0; j < times; j++) {
res.append(s.substring(preOpenIndex + 1, i));
}
res.insert(0, s.substring(0, intIndex));
res.append(s.substring(i + 1));
break;
}
}
s = res.toString();
}
return s;
}
public static void main(String[] args) {
System.out.println(decodeString("3[a2[c]]"));
}
}