Skip to content

Commit 70d0d80

Browse files
committed
Added task 394.
1 parent 825a0d3 commit 70d0d80

File tree

3 files changed

+101
-0
lines changed

3 files changed

+101
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package g0301_0400.s0394_decode_string;
2+
3+
// #Medium #Top_100_Liked_Questions #String #Stack #Recursion
4+
5+
public class Solution {
6+
private int i = 0;
7+
8+
public String decodeString(String s) {
9+
int count = 0;
10+
StringBuilder sb = new StringBuilder();
11+
while (i < s.length()) {
12+
char c = s.charAt(i);
13+
i++;
14+
if (Character.isLetter(c)) {
15+
sb.append(c);
16+
} else if (Character.isDigit(c)) {
17+
count = count * 10 + Character.getNumericValue(c);
18+
} else if (c == ']') {
19+
break;
20+
} else if (c == '[') {
21+
// sub problem
22+
String repeat = decodeString(s);
23+
while (count > 0) {
24+
sb.append(repeat);
25+
count--;
26+
}
27+
}
28+
}
29+
return sb.toString();
30+
}
31+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
394\. Decode String
2+
3+
Medium
4+
5+
Given an encoded string, return its decoded string.
6+
7+
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.
8+
9+
You may assume that the input string is always valid; No extra white spaces, square brackets are well-formed, etc.
10+
11+
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]`.
12+
13+
**Example 1:**
14+
15+
**Input:** s = "3\[a\]2\[bc\]"
16+
17+
**Output:** "aaabcbc"
18+
19+
**Example 2:**
20+
21+
**Input:** s = "3\[a2\[c\]\]"
22+
23+
**Output:** "accaccacc"
24+
25+
**Example 3:**
26+
27+
**Input:** s = "2\[abc\]3\[cd\]ef"
28+
29+
**Output:** "abcabccdcdcdef"
30+
31+
**Example 4:**
32+
33+
**Input:** s = "abc3\[cd\]xyz"
34+
35+
**Output:** "abccdcdcdxyz"
36+
37+
**Constraints:**
38+
39+
* `1 <= s.length <= 30`
40+
* `s` consists of lowercase English letters, digits, and square brackets `'[]'`.
41+
* `s` is guaranteed to be **a valid** input.
42+
* All the integers in `s` are in the range `[1, 300]`.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package g0301_0400.s0394_decode_string;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void decodeString() {
11+
assertThat(new Solution().decodeString("3[a]2[bc]"), equalTo("aaabcbc"));
12+
}
13+
14+
@Test
15+
void decodeString2() {
16+
assertThat(new Solution().decodeString("3[a2[c]]"), equalTo("accaccacc"));
17+
}
18+
19+
@Test
20+
void decodeString3() {
21+
assertThat(new Solution().decodeString("2[abc]3[cd]ef"), equalTo("abcabccdcdcdef"));
22+
}
23+
24+
@Test
25+
void decodeString4() {
26+
assertThat(new Solution().decodeString("abc3[cd]xyz"), equalTo("abccdcdcdxyz"));
27+
}
28+
}

0 commit comments

Comments
 (0)