Skip to content

Commit b5723b9

Browse files
committed
Runtime: 1 ms (Top 88.88%) | Memory: 41.8 MB (Top 72.85%)
1 parent 8b12034 commit b5723b9

File tree

1 file changed

+17
-16
lines changed

1 file changed

+17
-16
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
1+
// Runtime: 1 ms (Top 88.88%) | Memory: 41.8 MB (Top 72.85%)
12
class Solution {
23
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
56
int nbb = s.indexOf('[', bb + 1); // location of next beginning bracket
67
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
910
int nl = 1; // number of digits for n
1011
char nd; // next digit
11-
12-
String insert = ""; // repeated string
12+
13+
String insert = ""; // repeated string
1314
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
1819
bb = nbb; // update location of beginning bracket
1920
nbb = s.indexOf('[', bb + 1); // update location of next beginning bracket
2021
}
21-
22+
2223
nl = 1; // reset length of n
2324
while (bb - nl >= 0) { // while there are characters in front of the beginning bracket
2425
nd = s.charAt(bb - nl); // next digit
@@ -28,21 +29,21 @@ public String decodeString(String s) {
2829
}
2930
else break; // not an integer
3031
}
31-
32+
3233
insert = s.substring(bb + 1, eb); // set repeated string
3334
end = s.substring(eb + 1); // set remainder of string
3435
s = s.substring(0, bb - nl + 1); // remove everything after the digits
35-
36-
while (n > 0) {
36+
37+
while (n > 0) {
3738
s += insert; // add repeated string n times
3839
n--;
3940
}
4041
s += end; // add remainder of string
41-
42+
4243
bb = s.indexOf('['); // new location of beginning bracket
4344
nbb = s.indexOf('[', bb + 1); // new location of next beginning bracket
4445
eb = s.indexOf(']'); // new location of ending bracket
4546
}
4647
return s;
4748
}
48-
}
49+
}

0 commit comments

Comments
 (0)