1
+ // Runtime: 1 ms (Top 88.88%) | Memory: 41.8 MB (Top 72.85%)
1
2
class Solution {
2
3
public String decodeString (String s ) {
3
-
4
- int bb = s .indexOf ('[' ); // location of beginning bracket
4
+
5
+ int bb = s .indexOf ('[' ); // location of beginning bracket
5
6
int nbb = s .indexOf ('[' , bb + 1 ); // location of next beginning bracket
6
7
int eb = s .indexOf (']' ); // location of ending bracket
7
-
8
- int n = 0 ; // number of times to repeat
8
+
9
+ int n = 0 ; // number of times to repeat
9
10
int nl = 1 ; // number of digits for n
10
11
char nd ; // next digit
11
-
12
- String insert = "" ; // repeated string
12
+
13
+ String insert = "" ; // repeated string
13
14
String end = "" ; // remainder of string after repeated portion
14
-
15
- while (bb != -1 ) { // while the string contains a beginning bracket
16
-
17
- while (nbb < eb && nbb > bb ) { // while the next beginning bracket is before the ending bracket
15
+
16
+ while (bb != -1 ) { // while the string contains a beginning bracket
17
+
18
+ while (nbb < eb && nbb > bb ) { // while the next beginning bracket is before the ending bracket
18
19
bb = nbb ; // update location of beginning bracket
19
20
nbb = s .indexOf ('[' , bb + 1 ); // update location of next beginning bracket
20
21
}
21
-
22
+
22
23
nl = 1 ; // reset length of n
23
24
while (bb - nl >= 0 ) { // while there are characters in front of the beginning bracket
24
25
nd = s .charAt (bb - nl ); // next digit
@@ -28,21 +29,21 @@ public String decodeString(String s) {
28
29
}
29
30
else break ; // not an integer
30
31
}
31
-
32
+
32
33
insert = s .substring (bb + 1 , eb ); // set repeated string
33
34
end = s .substring (eb + 1 ); // set remainder of string
34
35
s = s .substring (0 , bb - nl + 1 ); // remove everything after the digits
35
-
36
- while (n > 0 ) {
36
+
37
+ while (n > 0 ) {
37
38
s += insert ; // add repeated string n times
38
39
n --;
39
40
}
40
41
s += end ; // add remainder of string
41
-
42
+
42
43
bb = s .indexOf ('[' ); // new location of beginning bracket
43
44
nbb = s .indexOf ('[' , bb + 1 ); // new location of next beginning bracket
44
45
eb = s .indexOf (']' ); // new location of ending bracket
45
46
}
46
47
return s ;
47
48
}
48
- }
49
+ }
0 commit comments