-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecodestring.java
51 lines (38 loc) · 1.42 KB
/
decodestring.java
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
49
50
51
class Solution {
public String decodeString(String s) {
char[] string = s.toCharArray();
return helper(string, 0);
}
private String helper(char[] string, int index) {
// base condition
if(index >= string.length) {
return "";
}
String result = "";
if(string[index] >= 'a' && string[index] <= 'z') {
result = string[index] + helper(string, index + 1);
}
if(string[index] == ']') {
// as soon as ']' is encontered, return the result
// Here, index will be used as a reference to proceed after ']'
return result + "-" + index;
}
String digit = "";
if(string[index] >= '0' && string[index] <= '9') {
while(index < string.length && string[index] >= '0' && string[index] <= '9') {
digit += string[index];
index ++;
}
}
if(digit.length() >= 1) {
int count = Integer.parseInt(digit);
String temp = helper(string, index + 1);
String[] temp2 = temp.split("-");
for(int i = 0; i < count; i++) {
result = result + temp2[0];
}
result = result + helper(string, Integer.parseInt(temp2[1]) + 1);
}
return result;
}
}