-
Notifications
You must be signed in to change notification settings - Fork 43
/
DecodeString.java
157 lines (142 loc) · 5.08 KB
/
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
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package LeetCodeJava.Stack;
// https://leetcode.com/problems/decode-string/description/
// https://leetcode.ca/all/394.html
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Stack;
/**
* 394. Decode String
* Given an encoded string, return its decoded string.
* <p>
* 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.
* <p>
* You may assume that the input string is always valid; No extra white spaces, square brackets are well-formed, etc.
* <p>
* 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].
* <p>
* Examples:
* <p>
* s = "3[a]2[bc]", return "aaabcbc".
* s = "3[a2[c]]", return "accaccacc".
* s = "2[abc]3[cd]ef", return "abcabccdcdcdef".
* <p>
* <p>
* Difficulty:
* Medium
* Lock:
* Normal
* Company:
* Amazon AppDynamics Apple Atlassian Bloomberg Cisco Coupang Cruise Automation Facebook Google Huawei Microsoft Oracle Salesforce Snapchat Tencent VMware Yahoo Yelp
*/
public class DecodeString {
// V0
// IDEA : STACK
// TODO : optimize code
public String decodeString(String s) {
if (s.isEmpty()) {
return null;
}
// init
Stack<String> stack = new Stack<>(); // ??
StringBuilder sb = new StringBuilder();
String A_TO_Z = "abcdefghijklmnopqrstuvwxyz";
for (String x : s.split("")) {
//System.out.println(">>> x = " + x);
String tmp = "";
StringBuilder tmpSb = new StringBuilder();
if (!x.equals("]")) {
if (!x.equals("[")) {
stack.add(x);
}
} else {
// pop all elements from stack, multiply, and add to res
while (!stack.isEmpty()) {
String cur = stack.pop(); // ??
if (A_TO_Z.contains(cur)) {
tmp = cur + tmp;
} else {
tmp = getMultiplyStr(tmp, Integer.parseInt(cur));
}
}
}
sb.append(tmp);
}
StringBuilder tmpSb = new StringBuilder();
// add remaining stack element to result
while (!stack.isEmpty()) {
tmpSb.append(stack.pop());
}
sb.append(tmpSb.reverse());
return sb.toString();
}
private String getMultiplyStr(String cur, Integer multiply) {
StringBuilder sb = new StringBuilder();
for (int x = 0; x < multiply; x++) {
sb.append(cur);
}
return sb.toString();
}
// V1
// https://leetcode.ca/2016-12-28-394-Decode-String/
public String decodeString_1(String s) {
Deque<Integer> s1 = new ArrayDeque<>();
Deque<String> s2 = new ArrayDeque<>();
int num = 0;
String res = "";
for (char c : s.toCharArray()) {
if ('0' <= c && c <= '9') {
num = num * 10 + c - '0';
} else if (c == '[') {
s1.push(num);
s2.push(res);
num = 0;
res = "";
} else if (c == ']') {
StringBuilder t = new StringBuilder();
for (int i = 0, n = s1.pop(); i < n; ++i) {
t.append(res);
}
res = s2.pop() + t.toString();
} else {
res += String.valueOf(c);
}
}
return res;
}
// V2
// IDEA : STACK (gpt)
public static String decodeString_2(String s) {
if (s.isEmpty()) {
return "";
}
// Stacks for numbers and strings
Stack<Integer> countStack = new Stack<>();
Stack<StringBuilder> stringStack = new Stack<>();
StringBuilder currentString = new StringBuilder();
int k = 0; // Temporary variable for number parsing
for (char ch : s.toCharArray()) {
if (Character.isDigit(ch)) {
// Build the number (support for multi-digit numbers)
k = k * 10 + (ch - '0');
} else if (ch == '[') {
// Push current number and string onto their respective stacks
countStack.push(k);
stringStack.push(currentString);
currentString = new StringBuilder(); // Start a new substring
k = 0; // Reset the number
} else if (ch == ']') {
// Decode substring
int count = countStack.pop();
StringBuilder decodedString = stringStack.pop();
for (int i = 0; i < count; i++) {
decodedString.append(currentString);
}
currentString = decodedString; // Update current string
} else {
// Append characters to current string
currentString.append(ch);
}
}
return currentString.toString();
}
}