Skip to content

Commit 36890ec

Browse files
committed
Java solution 394 && 402
1 parent 1fdb5d9 commit 36890ec

File tree

2 files changed

+121
-0
lines changed

2 files changed

+121
-0
lines changed

java/_394DecodeString.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import java.util.Stack;
2+
3+
/**
4+
* Given an encoded string, return it's decoded string.
5+
* 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.
6+
* You may assume that the input string is always valid; No extra white spaces, square brackets are well-formed, etc.
7+
* 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].
8+
* <p>
9+
* Examples:
10+
* <p>
11+
* s = "3[a]2[bc]", return "aaabcbc".
12+
* s = "3[a2[c]]", return "accaccacc".
13+
* s = "2[abc]3[cd]ef", return "abcabccdcdcdef".
14+
* <p>
15+
* Created by drfish on 6/13/2017.
16+
*/
17+
public class _394DecodeString {
18+
public String decodeString(String s) {
19+
Stack<Integer> countStack = new Stack<>();
20+
Stack<String> stringStack = new Stack<>();
21+
int index = 0;
22+
String curr = "";
23+
while (index < s.length()) {
24+
char c = s.charAt(index);
25+
if (Character.isDigit(c)) {
26+
int start = index;
27+
while (Character.isDigit(s.charAt(index + 1))) {
28+
index++;
29+
}
30+
countStack.push(Integer.parseInt(s.substring(start, index + 1)));
31+
} else if (c == '[') {
32+
stringStack.push(curr);
33+
curr = "";
34+
} else if (c == ']') {
35+
StringBuilder sb = new StringBuilder(stringStack.pop());
36+
int repeatTimes = countStack.pop();
37+
for (int i = 0; i < repeatTimes; i++) {
38+
sb.append(curr);
39+
}
40+
curr = sb.toString();
41+
} else {
42+
curr += c;
43+
}
44+
index++;
45+
}
46+
return curr;
47+
}
48+
49+
public static void main(String[] args) {
50+
_394DecodeString solution = new _394DecodeString();
51+
assert "aaabcbc".equals(solution.decodeString("3[a]2[bc]"));
52+
assert "accaccacc".equals(solution.decodeString("3[a2[c]]"));
53+
assert "abcabccdcdcdef".equals(solution.decodeString("2[abc]3[cd]ef"));
54+
}
55+
}

java/_402RemoveKDigits.java

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import java.util.Stack;
2+
3+
/**
4+
* Given a non-negative integer num represented as a string, remove k digits from the number so that the new number is the smallest possible.
5+
* <p>
6+
* Note:
7+
* The length of num is less than 10002 and will be ≥ k.
8+
* The given num does not contain any leading zero.
9+
* <p>
10+
* Example 1:
11+
* Input: num = "1432219", k = 3
12+
* Output: "1219"
13+
* Explanation: Remove the three digits 4, 3, and 2 to form the new number 1219 which is the smallest.
14+
* <p>
15+
* Example 2:
16+
* Input: num = "10200", k = 1
17+
* Output: "200"
18+
* Explanation: Remove the leading 1 and the number is 200. Note that the output must not contain leading zeroes.
19+
* <p>
20+
* Example 3:
21+
* Input: num = "10", k = 2
22+
* Output: "0"
23+
* Explanation: Remove all the digits from the number and it is left with nothing which is 0.
24+
* <p>
25+
* Created by drfish on 6/13/2017.
26+
*/
27+
public class _402RemoveKDigits {
28+
public String removeKdigits(String num, int k) {
29+
if (num.length() <= k) {
30+
return "0";
31+
}
32+
Stack<Character> stack = new Stack<>();
33+
int index = 0;
34+
// remove digit which is bigger than its next one
35+
while (index < num.length()) {
36+
while (k > 0 && !stack.isEmpty() && stack.peek() > num.charAt(index)) {
37+
stack.pop();
38+
k--;
39+
}
40+
stack.push(num.charAt(index));
41+
index++;
42+
}
43+
while (k > 0) {
44+
stack.pop();
45+
k--;
46+
}
47+
// construct new number
48+
StringBuilder sb = new StringBuilder();
49+
while (!stack.isEmpty()) {
50+
sb.append(stack.pop());
51+
}
52+
sb.reverse();
53+
// remove zeros at the head
54+
while (sb.length() > 0 && sb.charAt(0) == '0') {
55+
sb.deleteCharAt(0);
56+
}
57+
return sb.toString();
58+
}
59+
60+
public static void main(String[] args) {
61+
_402RemoveKDigits solution = new _402RemoveKDigits();
62+
assert "1219".equals(solution.removeKdigits("1432219", 3));
63+
assert "200".equals(solution.removeKdigits("10200", 1));
64+
assert "0".equals(solution.removeKdigits("10", 2));
65+
}
66+
}

0 commit comments

Comments
 (0)