-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_1047.java
36 lines (33 loc) · 1.12 KB
/
_1047.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
package com.fishercoder.solutions.secondthousand;
import java.util.Deque;
import java.util.LinkedList;
public class _1047 {
public static class Solution1 {
public String removeDuplicates(String S) {
StringBuilder sb = new StringBuilder(S);
for (int i = 0; i < S.length() - 1; i++) {
if (S.charAt(i) == S.charAt(i + 1)) {
return removeDuplicates(S.substring(0, i) + S.substring(i + 2));
}
}
return sb.toString();
}
}
public static class Solution2 {
public String removeDuplicates(String s) {
Deque<Character> stack = new LinkedList<>();
for (char c : s.toCharArray()) {
if (!stack.isEmpty() && stack.peekLast() == c) {
stack.pollLast();
} else {
stack.addLast(c);
}
}
StringBuilder sb = new StringBuilder();
while (!stack.isEmpty()) {
sb.append(stack.pollLast());
}
return sb.reverse().toString();
}
}
}