/
_0394_DecodeString.java
62 lines (56 loc) · 2.06 KB
/
_0394_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
52
53
54
55
56
57
58
59
60
61
62
package com.diguage.algorithm.leetcode;
import java.util.Deque;
import java.util.LinkedList;
import java.util.Objects;
/**
* = 394. Decode String
*
* https://leetcode.com/problems/decode-string/[Decode String - LeetCode]
*
* @author D瓜哥, https://www.diguage.com/
* @since 2020-01-28 22:08
*/
public class _0394_DecodeString {
/**
* Runtime: 1 ms, faster than 64.03% of Java online submissions for Decode String.
* Memory Usage: 37.4 MB, less than 5.68% of Java online submissions for Decode String.
*/
public String decodeString(String s) {
if (Objects.isNull(s) || s.length() == 0) {
return "";
}
Deque<Integer> nums = new LinkedList<>();
Deque<String> strings = new LinkedList<>();
int num = 0;
StringBuilder result = new StringBuilder();
for (char c : s.toCharArray()) {
if (c == '[') {
nums.addLast(num);
strings.addLast(result.toString());
num = 0;
result = new StringBuilder();
} else if (c == ']') {
StringBuilder sb = new StringBuilder();
Integer n = nums.removeLast();
for (int i = 0; i < n; i++) {
sb.append(result);
}
result = new StringBuilder(strings.removeLast() + sb.toString());
} else if (Character.isDigit(c)) {
num = num * 10 + (c - '0');
} else if (Character.isLetter(c)) {
result.append(c);
}
}
return result.toString();
}
public static void main(String[] args) {
_0394_DecodeString solution = new _0394_DecodeString();
String r1 = solution.decodeString("3[a]2[bc]");
System.out.println("aaabcbc".equals(r1) + " : " + r1);
String r2 = solution.decodeString("3[a2[c]]");
System.out.println("accaccacc".equals(r2) + " : " + r2);
String r3 = solution.decodeString("2[abc]3[cd]ef");
System.out.println("abcabccdcdcdef".equals(r3) + " : " + r3);
}
}