Skip to content

Commit b509477

Browse files
committed
add 394 java, progress
1 parent 5d44b13 commit b509477

File tree

7 files changed

+350
-16
lines changed

7 files changed

+350
-16
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@
445445
331| [Verify Preorder Serialization of a Binary Tree](https://leetcode.com/problems/verify-preorder-serialization-of-a-binary-tree/) | [Python](./leetcode_python/Stack/verify-preorder-serialization-of-a-binary-tree.py) | _O(n)_| _O(1)_| Medium || AGAIN (not start)
446446
341| [Flatten Nested List Iterator](https://leetcode.com/problems/flatten-nested-list-iterator/)| [Python](./leetcode_python/Stack/flatten-nested-list-iterator.py) | _O(n)_ | _O(h)_ | Medium |LC 284, 🔒 Iterator, stack, generator, `good basic`,`amazon`,`fb***` | AGAIN********* (6)
447447
385| [Mini Parser](https://leetcode.com/problems/mini-parser/)| [Python](./leetcode_python/Stack/mini-parser.py) | _O(n)_ | _O(h)_ | Medium || AGAIN
448-
394| [Decode String](https://leetcode.com/problems/decode-string/)| [Python](./leetcode_python/Stack/decode-string.py) | _O(n)_ | _O(h)_ | Medium |stack, good basic!!!!, pre num string, LC 224, LC 227, `amazon`, `google`| AGAIN************** (6) (MUST)
448+
394| [Decode String](https://leetcode.com/problems/decode-string/)| [Python](./leetcode_python/Stack/decode-string.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Stack/DecodeString.java) | _O(n)_ | _O(h)_ | Medium |stack, good basic!!!!, pre num string, LC 224, LC 227, `amazon`, `google`| AGAIN************** (7) (MUST)
449449
439| [Ternary Expression Parser](https://leetcode.com/problems/ternary-expression-parser/) | [Python](./leetcode_python/Stack/ternary-expression-parser.py)| _O(n)_ | _O(1)_ | Medium |🔒| AGAIN (not start)
450450
456| [132 Pattern](https://leetcode.com/problems/132-pattern/) | [Python](./leetcode_python/Stack/132-pattern.py) | _O(n)_ | _O(n)_ | Medium || AGAIN (not start)
451451
496| [Next Greater Element I](https://leetcode.com/problems/next-greater-element-i/) | [Python](./leetcode_python/Stack/next_greater_element_i.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Stack/NextGreaterElement_I.java)| _O(n)_| _O(n)_| Easy|good basic, stack,monotonic stack, LC 739, LC 503, LC 406| OK********* (5) (but again)

data/progress.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Progress
22

3+
# 2024-11-17
4+
- https://github.com/yennanliu/CS_basics/blob/master/doc/Leetcode_company_frequency-master/Google%206months-%20LeetCode.pdf
5+
36
# 2024-11-16
47
- https://github.com/yennanliu/CS_basics/blob/master/doc/Leetcode_company_frequency-master/Google%206months-%20LeetCode.pdf
58

data/progress.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
20241117: 394
12
20241116: 833,950
23
20241114: 80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)
34
20241110: 53,210,207

data/to_review.txt

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,35 @@
1+
2025-01-11 -> ['394']
12
2025-01-10 -> ['833,950']
23
2025-01-08 -> ['80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
34
2025-01-04 -> ['53,210,207']
45
2025-01-03 -> ['444']
56
2025-01-02 -> ['1188,130,855(again)']
67
2024-12-28 -> ['900']
78
2024-12-27 -> ['26,27', '802,1197,26']
8-
2024-12-21 -> ['855,846']
9+
2024-12-21 -> ['394', '855,846']
910
2024-12-20 -> ['833,950', '932']
1011
2024-12-18 -> ['80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)', '951,792']
1112
2024-12-14 -> ['53,210,207', '163,1048']
1213
2024-12-13 -> ['444', '298,729']
1314
2024-12-12 -> ['1188,130,855(again)', '1146']
14-
2024-12-08 -> ['737']
15+
2024-12-08 -> ['394', '737']
1516
2024-12-07 -> ['833,950', '900', '686,734,737']
1617
2024-12-06 -> ['26,27', '802,1197,26', '353']
1718
2024-12-05 -> ['80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)', '528,334']
1819
2024-12-03 -> ['1145']
1920
2024-12-01 -> ['53,210,207']
20-
2024-11-30 -> ['444', '855,846', '1145,1219']
21+
2024-11-30 -> ['394', '444', '855,846', '1145,1219']
2122
2024-11-29 -> ['833,950', '1188,130,855(again)', '932']
2223
2024-11-27 -> ['80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)', '951,792', '524,221,889']
2324
2024-11-26 -> ['743,889']
24-
2024-11-25 -> ['837']
25+
2024-11-25 -> ['394', '837']
2526
2024-11-24 -> ['833,950', '900']
2627
2024-11-23 -> ['53,210,207', '26,27', '802,1197,26', '163,1048', '981']
27-
2024-11-22 -> ['80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)', '444', '298,729', '1087']
28+
2024-11-22 -> ['394', '80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)', '444', '298,729', '1087']
2829
2024-11-21 -> ['833,950', '1188,130,855(again)', '1146']
29-
2024-11-20 -> ['939']
30-
2024-11-19 -> ['833,950', '80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
31-
2024-11-18 -> ['833,950', '53,210,207', '430']
30+
2024-11-20 -> ['394', '939']
31+
2024-11-19 -> ['394', '833,950', '80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
32+
2024-11-18 -> ['394', '833,950', '53,210,207', '430']
3233
2024-11-17 -> ['833,950', '80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)', '444', '855,846', '737', '363']
3334
2024-11-16 -> ['80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)', '1188,130,855(again)', '900', '932', '686,734,737', '1032,844,1011']
3435
2024-11-15 -> ['80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)', '53,210,207', '26,27', '802,1197,26', '353', '947']
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
package LeetCodeJava.Stack;
2+
3+
// https://leetcode.com/problems/decode-string/description/
4+
// https://leetcode.ca/all/394.html
5+
6+
import java.util.ArrayDeque;
7+
import java.util.Deque;
8+
import java.util.Stack;
9+
10+
/**
11+
* 394. Decode String
12+
* Given an encoded string, return its decoded string.
13+
* <p>
14+
* 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.
15+
* <p>
16+
* You may assume that the input string is always valid; No extra white spaces, square brackets are well-formed, etc.
17+
* <p>
18+
* 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].
19+
* <p>
20+
* Examples:
21+
* <p>
22+
* s = "3[a]2[bc]", return "aaabcbc".
23+
* s = "3[a2[c]]", return "accaccacc".
24+
* s = "2[abc]3[cd]ef", return "abcabccdcdcdef".
25+
* <p>
26+
* <p>
27+
* Difficulty:
28+
* Medium
29+
* Lock:
30+
* Normal
31+
* Company:
32+
* Amazon AppDynamics Apple Atlassian Bloomberg Cisco Coupang Cruise Automation Facebook Google Huawei Microsoft Oracle Salesforce Snapchat Tencent VMware Yahoo Yelp
33+
*/
34+
public class DecodeString {
35+
36+
// V0
37+
// IDEA : STACK
38+
// TODO : optimize code
39+
public String decodeString(String s) {
40+
if (s.isEmpty()) {
41+
return null;
42+
}
43+
// init
44+
Stack<String> stack = new Stack<>(); // ??
45+
StringBuilder sb = new StringBuilder();
46+
String A_TO_Z = "abcdefghijklmnopqrstuvwxyz";
47+
for (String x : s.split("")) {
48+
//System.out.println(">>> x = " + x);
49+
String tmp = "";
50+
StringBuilder tmpSb = new StringBuilder();
51+
if (!x.equals("]")) {
52+
if (!x.equals("[")) {
53+
stack.add(x);
54+
}
55+
} else {
56+
// pop all elements from stack, multiply, and add to res
57+
while (!stack.isEmpty()) {
58+
String cur = stack.pop(); // ??
59+
if (A_TO_Z.contains(cur)) {
60+
tmp = cur + tmp;
61+
} else {
62+
tmp = getMultiplyStr(tmp, Integer.parseInt(cur));
63+
}
64+
}
65+
}
66+
sb.append(tmp);
67+
}
68+
69+
StringBuilder tmpSb = new StringBuilder();
70+
71+
// add remaining stack element to result
72+
while (!stack.isEmpty()) {
73+
tmpSb.append(stack.pop());
74+
}
75+
76+
sb.append(tmpSb.reverse());
77+
return sb.toString();
78+
}
79+
80+
private String getMultiplyStr(String cur, Integer multiply) {
81+
StringBuilder sb = new StringBuilder();
82+
for (int x = 0; x < multiply; x++) {
83+
sb.append(cur);
84+
}
85+
return sb.toString();
86+
}
87+
88+
89+
// V1
90+
// https://leetcode.ca/2016-12-28-394-Decode-String/
91+
public String decodeString_1(String s) {
92+
Deque<Integer> s1 = new ArrayDeque<>();
93+
Deque<String> s2 = new ArrayDeque<>();
94+
int num = 0;
95+
String res = "";
96+
for (char c : s.toCharArray()) {
97+
if ('0' <= c && c <= '9') {
98+
num = num * 10 + c - '0';
99+
} else if (c == '[') {
100+
s1.push(num);
101+
s2.push(res);
102+
num = 0;
103+
res = "";
104+
} else if (c == ']') {
105+
StringBuilder t = new StringBuilder();
106+
for (int i = 0, n = s1.pop(); i < n; ++i) {
107+
t.append(res);
108+
}
109+
res = s2.pop() + t.toString();
110+
} else {
111+
res += String.valueOf(c);
112+
}
113+
}
114+
return res;
115+
}
116+
117+
// V2
118+
// IDEA : STACK (gpt)
119+
public static String decodeString_2(String s) {
120+
if (s.isEmpty()) {
121+
return "";
122+
}
123+
124+
// Stacks for numbers and strings
125+
Stack<Integer> countStack = new Stack<>();
126+
Stack<StringBuilder> stringStack = new Stack<>();
127+
StringBuilder currentString = new StringBuilder();
128+
int k = 0; // Temporary variable for number parsing
129+
130+
for (char ch : s.toCharArray()) {
131+
if (Character.isDigit(ch)) {
132+
// Build the number (support for multi-digit numbers)
133+
k = k * 10 + (ch - '0');
134+
} else if (ch == '[') {
135+
// Push current number and string onto their respective stacks
136+
countStack.push(k);
137+
stringStack.push(currentString);
138+
currentString = new StringBuilder(); // Start a new substring
139+
k = 0; // Reset the number
140+
} else if (ch == ']') {
141+
// Decode substring
142+
int count = countStack.pop();
143+
StringBuilder decodedString = stringStack.pop();
144+
for (int i = 0; i < count; i++) {
145+
decodedString.append(currentString);
146+
}
147+
currentString = decodedString; // Update current string
148+
} else {
149+
// Append characters to current string
150+
currentString.append(ch);
151+
}
152+
}
153+
154+
return currentString.toString();
155+
}
156+
157+
}

0 commit comments

Comments
 (0)