-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cs
More file actions
32 lines (29 loc) · 1023 Bytes
/
Solution.cs
File metadata and controls
32 lines (29 loc) · 1023 Bytes
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
namespace LeetCode.Problem1047{
//1047. Remove All Adjacent Duplicates In String
//https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/
/*
You are given a string s consisting of lowercase English letters.
A duplicate removal consists of choosing two adjacent and equal letters and removing them.
We repeatedly make duplicate removals on s until we no longer can.
Return the final string after all such duplicate removals have been made. It can be proven that the answer is unique.
*/
public class Solution {
public string RemoveDuplicates(string s) {
Stack<char> word = new Stack<char>();
for (int i = 0; i < s.Length; i++)
{
if (word.Count > 0)
{
var last = word.Peek();
if (last == s[i])
word.Pop();
else
word.Push(s[i]);
}
else
word.Push(s[i]);
}
return new string(word.Reverse().ToArray());
}
}
}