Skip to content

Commit af2c430

Browse files
add 3222
1 parent 1e62256 commit af2c430

File tree

3 files changed

+72
-0
lines changed
  • paginated_contents/algorithms/4th_thousand
  • src

3 files changed

+72
-0
lines changed

Diff for: paginated_contents/algorithms/4th_thousand/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
| # | Title | Solutions | Video | Difficulty | Tag
22
|------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------|------------|----------------------------------------------------------------------
3+
| 3223 | [Minimum Length of String After Operations](https://leetcode.com/problems/minimum-length-of-string-after-operations/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3223.java) | | Medium |
34
| 3222 | [Find the Winning Player in Coin Game](https://leetcode.com/problems/find-the-winning-player-in-coin-game/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3222.java) | | Easy |
45
| 3219 | [Minimum Cost for Cutting Cake II](https://leetcode.com/problems/minimum-cost-for-cutting-cake-ii/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3219.java) | | Hard | Greedy
56
| 3218 | [Minimum Cost for Cutting Cake I](https://leetcode.com/problems/minimum-cost-for-cutting-cake-i/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3218.java) | | Medium |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.fishercoder.solutions.fourththousand;
2+
3+
import java.util.Deque;
4+
import java.util.HashMap;
5+
import java.util.LinkedList;
6+
import java.util.Map;
7+
8+
public class _3223 {
9+
public static class Solution1 {
10+
public int minimumLength(String s) {
11+
Map<Character, Integer> map = new HashMap<>();
12+
Deque<Character> stack = new LinkedList<>();
13+
for (int i = 0; i < s.length(); i++) {
14+
char c = s.charAt(i);
15+
if (!map.containsKey(c) || map.get(c) < 2) {
16+
map.put(c, map.getOrDefault(c, 0) + 1);
17+
stack.addLast(c);
18+
} else {
19+
Deque<Character> tmpStack = new LinkedList<>();
20+
int removedCount = 0;
21+
while (!stack.isEmpty()) {
22+
Character last = stack.pollLast();
23+
if (last == c) {
24+
map.put(c, map.get(c) - 1);
25+
if (map.get(c) == 0) {
26+
map.remove(c);
27+
}
28+
removedCount++;
29+
if (removedCount == 2) {
30+
break;
31+
}
32+
} else {
33+
tmpStack.addLast(last);
34+
}
35+
}
36+
while (!tmpStack.isEmpty()) {
37+
stack.addLast(tmpStack.pollLast());
38+
}
39+
stack.addLast(c);
40+
map.put(c, map.getOrDefault(c, 0) + 1);
41+
}
42+
}
43+
StringBuilder sb = new StringBuilder();
44+
while (!stack.isEmpty()) {
45+
sb.append(stack.pollFirst());
46+
}
47+
return sb.toString().length();
48+
}
49+
}
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.fishercoder.fourththousand;
2+
3+
import com.fishercoder.solutions.fourththousand._3223;
4+
import org.junit.jupiter.api.BeforeEach;
5+
import org.junit.jupiter.api.Test;
6+
7+
import static org.junit.jupiter.api.Assertions.assertEquals;
8+
9+
public class _3223Test {
10+
private static _3223.Solution1 solution1;
11+
12+
@BeforeEach
13+
public void setup() {
14+
solution1 = new _3223.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(38, solution1.minimumLength("ucvbutgkohgbcobqeyqwppbxqoynxeuuzouyvmydfhrprdbuzwqebwuiejoxsxdhbmuaiscalnteocghnlisxxawxgcjloevrdcj"));
20+
}
21+
}

0 commit comments

Comments
 (0)