Skip to content

Commit d68bd9d

Browse files
committed
Remove All Adjacent Duplicates In String
1 parent f00767c commit d68bd9d

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"""
2+
Problem Link: https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/
3+
4+
Given a string S of lowercase letters, a duplicate removal consists of choosing two adjacent
5+
and equal letters, and removing them.
6+
We repeatedly make duplicate removals on S until we no longer can.
7+
Return the final string after all such duplicate removals have been made.
8+
It is guaranteed the answer is unique.
9+
10+
Example 1:
11+
Input: "abbaca"
12+
Output: "ca"
13+
Explanation:
14+
For example, in "abbaca" we could remove "bb" since the letters are adjacent and equal,
15+
and this is the only possible move. The result of this move is that the string is "aaca",
16+
of which only "aa" is possible, so the final string is "ca".
17+
18+
Note:
19+
1 <= S.length <= 20000
20+
S consists only of English lowercase letters.
21+
"""
22+
class Solution:
23+
def removeDuplicates(self, S: str) -> str:
24+
stack = []
25+
for c in S:
26+
if not stack or stack[-1] != c:
27+
stack.append(c)
28+
else:
29+
stack.pop()
30+
return "".join(stack)

0 commit comments

Comments
 (0)