-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path394_Decode_String.java
More file actions
116 lines (107 loc) · 3.61 KB
/
Copy path394_Decode_String.java
File metadata and controls
116 lines (107 loc) · 3.61 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
* 394. Decode String
* Target: Given an encoded string, return its decoded string. Note that k is guaranteed to be a positive integer.
* The encoding rule is: k[encoded_string], where the encoded_string inside the square brackets is being repeated exactly k times.
* You may assume that the input string is always valid; No extra white spaces, square brackets are well-formed, etc.
* Furthermore, you may assume that the original data does not contain any digits and that digits are only for those repeat numbers, k.
* Difficulty:Medium
* Classification:Greedy
*/
/*
* Solution 1
* 2019-08-03 Runtime: 0 ms
* Algorithm: => Stack. Use one stack to store int and string.
* Time Complexity: ?. Space Conplexity: O(n).
*/
class Solution {
public String decodeString(String s) {
String res = "";
Deque<String> stack = new ArrayDeque<>();
int i = 0;
while (i < s.length()) {
if (s.charAt(i) >= '0' && s.charAt(i) <= '9') {
stack.push(res);
int start = i;
while (s.charAt(i + 1) >= '0' && s.charAt(i + 1) <= '9') i++;
stack.push(s.substring(start, i + 1));
} else if (s.charAt(i) == '[') {
res = "";
} else if (s.charAt(i) == ']') {
int times = Integer.parseInt(stack.pop());
StringBuilder sb = new StringBuilder(stack.pop());
for (int j = 0; j < times; j++) {
sb.append(res);
}
res = sb.toString();
} else {
res += s.charAt(i);
}
i++;
}
return res;
}
}
/*
* Solution 2
* 2019-08-03 Runtime: 1 ms
* Algorithm: => Stack. Use two stack to store int and string separately.
* Time Complexity: ?. Space Conplexity: O(n).
*/
class Solution {
public String decodeString(String s) {
StringBuilder res = new StringBuilder();
Deque<Integer> intStack = new ArrayDeque<>();
Deque<StringBuilder> strStack = new ArrayDeque<>();
int i = 0;
for (char ch : s.toCharArray()) {
if (Character.isDigit(ch)) {
i = i * 10 + ch - '0';
} else if (ch == '[') {
intStack.push(i);
strStack.push(res);
res = new StringBuilder();
i = 0;
} else if (ch == ']') {
StringBuilder sb = res;
res = strStack.pop();
int start = intStack.pop();
for (int j = start; j > 0; j--) {
res.append(sb);
}
} else {
res.append(ch);
}
}
return res.toString();
}
}
/*
* Solution 3
* 2019-08-03 Runtime: 0 ms
* Algorithm: => DFS. Use a global variable k.
* Time Complexity: ?. Space Conplexity: ?.
*/
class Solution {
private int k = 0;
public String decodeString(String s) {
StringBuilder res = new StringBuilder();
int i = 0;
for (; k < s.length(); k++) {
if (Character.isDigit(s.charAt(k))) {
i = i * 10 + s.charAt(k) - '0';
} else if (s.charAt(k) == '[') {
k++;
String tmp = decodeString(s);
for (int j = 0; j < i; j++) {
res.append(tmp);
}
i = 0;
} else if (s.charAt(k) == ']') {
return res.toString();
} else {
res.append(s.charAt(k));
}
}
return res.toString();
}
}