Skip to content

Commit

Permalink
add 394 java, progress
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Nov 17, 2024
1 parent 5d44b13 commit b509477
Show file tree
Hide file tree
Showing 7 changed files with 350 additions and 16 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@
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)
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)
385| [Mini Parser](https://leetcode.com/problems/mini-parser/)| [Python](./leetcode_python/Stack/mini-parser.py) | _O(n)_ | _O(h)_ | Medium || AGAIN
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)
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)
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)
456| [132 Pattern](https://leetcode.com/problems/132-pattern/) | [Python](./leetcode_python/Stack/132-pattern.py) | _O(n)_ | _O(n)_ | Medium || AGAIN (not start)
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)
Expand Down
3 changes: 3 additions & 0 deletions data/progress.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Progress

# 2024-11-17
- https://github.com/yennanliu/CS_basics/blob/master/doc/Leetcode_company_frequency-master/Google%206months-%20LeetCode.pdf

# 2024-11-16
- https://github.com/yennanliu/CS_basics/blob/master/doc/Leetcode_company_frequency-master/Google%206months-%20LeetCode.pdf

Expand Down
1 change: 1 addition & 0 deletions data/progress.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
20241117: 394
20241116: 833,950
20241114: 80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)
20241110: 53,210,207
Expand Down
17 changes: 9 additions & 8 deletions data/to_review.txt
Original file line number Diff line number Diff line change
@@ -1,34 +1,35 @@
2025-01-11 -> ['394']
2025-01-10 -> ['833,950']
2025-01-08 -> ['80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
2025-01-04 -> ['53,210,207']
2025-01-03 -> ['444']
2025-01-02 -> ['1188,130,855(again)']
2024-12-28 -> ['900']
2024-12-27 -> ['26,27', '802,1197,26']
2024-12-21 -> ['855,846']
2024-12-21 -> ['394', '855,846']
2024-12-20 -> ['833,950', '932']
2024-12-18 -> ['80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)', '951,792']
2024-12-14 -> ['53,210,207', '163,1048']
2024-12-13 -> ['444', '298,729']
2024-12-12 -> ['1188,130,855(again)', '1146']
2024-12-08 -> ['737']
2024-12-08 -> ['394', '737']
2024-12-07 -> ['833,950', '900', '686,734,737']
2024-12-06 -> ['26,27', '802,1197,26', '353']
2024-12-05 -> ['80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)', '528,334']
2024-12-03 -> ['1145']
2024-12-01 -> ['53,210,207']
2024-11-30 -> ['444', '855,846', '1145,1219']
2024-11-30 -> ['394', '444', '855,846', '1145,1219']
2024-11-29 -> ['833,950', '1188,130,855(again)', '932']
2024-11-27 -> ['80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)', '951,792', '524,221,889']
2024-11-26 -> ['743,889']
2024-11-25 -> ['837']
2024-11-25 -> ['394', '837']
2024-11-24 -> ['833,950', '900']
2024-11-23 -> ['53,210,207', '26,27', '802,1197,26', '163,1048', '981']
2024-11-22 -> ['80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)', '444', '298,729', '1087']
2024-11-22 -> ['394', '80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)', '444', '298,729', '1087']
2024-11-21 -> ['833,950', '1188,130,855(again)', '1146']
2024-11-20 -> ['939']
2024-11-19 -> ['833,950', '80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
2024-11-18 -> ['833,950', '53,210,207', '430']
2024-11-20 -> ['394', '939']
2024-11-19 -> ['394', '833,950', '80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
2024-11-18 -> ['394', '833,950', '53,210,207', '430']
2024-11-17 -> ['833,950', '80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)', '444', '855,846', '737', '363']
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']
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']
Expand Down
157 changes: 157 additions & 0 deletions leetcode_java/src/main/java/LeetCodeJava/Stack/DecodeString.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,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();
}

}
Loading

0 comments on commit b509477

Please sign in to comment.