Skip to content

Commit f167400

Browse files
committed
1047. Remove All Adjacent Duplicates In String
1 parent f39bf68 commit f167400

File tree

2 files changed

+46
-7
lines changed

2 files changed

+46
-7
lines changed

README.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -303,13 +303,14 @@ The project is divided into two parts: `structure` and `solution`.
303303

304304
#### [Stack](src/leetcode/solution/stack)
305305

306-
| No. | Title | Difficulty | Solution | Idea |
307-
| ---- | ------------------------------------------------------------ | ---------- | ------------------------------------------------------------ | ----------------------- |
308-
| 32 | [Longest Valid Parentheses](https://leetcode.com/problems/longest-valid-parentheses/) | Hard | [LongestValidParentheses.java](src/leetcode/solution/stack/LongestValidParentheses.java) | Stack |
309-
| 316 | [Remove Duplicate Letters](https://leetcode.com/problems/remove-duplicate-letters/) | Medium | [RemoveDuplicateLetters.java](src/leetcode/solution/stack/RemoveDuplicateLetters.java) | Stack |
310-
| 394 | [Decode String](https://leetcode.com/problems/decode-string/) | Medium | [DecodeString.java](src/leetcode/solution/stack/DecodeString.java) | Two Stack |
311-
| 921 | [Minimum Add to Make Parentheses Valid](https://leetcode.com/problems/minimum-add-to-make-parentheses-valid/) | Medium | [MinimumAddToMakeParenthesesValid.java](src/leetcode/solution/stack/MinimumAddToMakeParenthesesValid.java) | Balance |
312-
| 1249 | [Minimum Remove to Make Valid Parentheses](https://leetcode.com/problems/minimum-remove-to-make-valid-parentheses/) | Medium | [MinimumRemoveToMakeValidParentheses.java](src/leetcode/solution/stack/MinimumRemoveToMakeValidParentheses.java) | Using Stack to save '(' |
306+
| No. | Title | Difficulty | Solution | Idea |
307+
| ---- | ------------------------------------------------------------ | ---------- | ------------------------------------------------------------ | --------------------------- |
308+
| 32 | [Longest Valid Parentheses](https://leetcode.com/problems/longest-valid-parentheses/) | Hard | [LongestValidParentheses.java](src/leetcode/solution/stack/LongestValidParentheses.java) | Stack |
309+
| 316 | [Remove Duplicate Letters](https://leetcode.com/problems/remove-duplicate-letters/) | Medium | [RemoveDuplicateLetters.java](src/leetcode/solution/stack/RemoveDuplicateLetters.java) | Stack |
310+
| 394 | [Decode String](https://leetcode.com/problems/decode-string/) | Medium | [DecodeString.java](src/leetcode/solution/stack/DecodeString.java) | Two Stack |
311+
| 921 | [Minimum Add to Make Parentheses Valid](https://leetcode.com/problems/minimum-add-to-make-parentheses-valid/) | Medium | [MinimumAddToMakeParenthesesValid.java](src/leetcode/solution/stack/MinimumAddToMakeParenthesesValid.java) | Balance |
312+
| 1047 | [Remove All Adjacent Duplicates In String](https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/) | Easy | [RemoveAllAdjacentDuplicatesInString.java](src/leetcode/solution/stack/RemoveAllAdjacentDuplicatesInString.java) | Use Stack or StringBuilder. |
313+
| 1249 | [Minimum Remove to Make Valid Parentheses](https://leetcode.com/problems/minimum-remove-to-make-valid-parentheses/) | Medium | [MinimumRemoveToMakeValidParentheses.java](src/leetcode/solution/stack/MinimumRemoveToMakeValidParentheses.java) | Using Stack to save '(' |
313314

314315
#### [Math](src/leetcode/solution/math)
315316

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package leetcode.solution.stack;
2+
3+
import java.util.Stack;
4+
5+
/**
6+
* 1047. Remove All Adjacent Duplicates In String
7+
*/
8+
public class RemoveAllAdjacentDuplicatesInString {
9+
10+
public static void main(String[] args) {
11+
String s = "abbaca";
12+
RemoveAllAdjacentDuplicatesInString decodeString = new RemoveAllAdjacentDuplicatesInString();
13+
System.out.println(decodeString.removeDuplicates(s));
14+
// Output: "ca"
15+
}
16+
17+
public String removeDuplicates(String s) {
18+
Stack<Character> stack = new Stack<>();
19+
char[] chars = s.toCharArray();
20+
21+
for (char c : chars) {
22+
if (stack.isEmpty() || stack.peek() != c) {
23+
stack.push(c);
24+
} else {
25+
stack.pop();
26+
}
27+
}
28+
29+
StringBuilder stringBuilder = new StringBuilder();
30+
for (Character c : stack) {
31+
stringBuilder.append(c);
32+
}
33+
34+
return stringBuilder.toString();
35+
36+
}
37+
38+
}

0 commit comments

Comments
 (0)