---
# System prepended metadata

title: 1047. Remove All Adjacent Duplicates In String
tags: [Leetcode, FaceBook, Easy, Stack]

---

# 1047. Remove All Adjacent Duplicates In String
###### tags: `Leetcode` `Easy` `FaceBook` `Stack`
Link: https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/
## 思路
直觉上是stack，但是对于String来说 StringBuilder和StringBuffer已经可以实现Stack的功能，因此不需要先用stack，再用StringBuffer
## Code
```java=
class Solution {
    public String removeDuplicates(String s) {
        StringBuilder stringBuilder = new StringBuilder();
        int top = -1;
        for(int i = 0;i < s.length();i++){
            if(top>=0 && stringBuilder.charAt(top)==s.charAt(i)){
                stringBuilder.deleteCharAt(top);
                top--;
            }
            else{
                stringBuilder.append(s.charAt(i));
                top++;
            }
        }
        
        return stringBuilder.toString();
    }
}
```
## Result
Runtime: 9 ms, faster than **81.90%** of Java online submissions for Remove All Adjacent Duplicates In String.
Memory Usage: 39.4 MB, less than **90.79%** of Java online submissions for Remove All Adjacent Duplicates In String.