-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cs
More file actions
35 lines (32 loc) · 846 Bytes
/
Solution.cs
File metadata and controls
35 lines (32 loc) · 846 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
33
34
35
public class Solution
{
public int MinLength(string s)
{
while (s.Contains("AB", StringComparison.CurrentCulture) || s.Contains("CD", StringComparison.CurrentCulture))
{
s = s.Replace("AB", string.Empty).Replace("CD", string.Empty);
}
return s.Length;
}
private int MinLength_Stack(string s)
{
Stack<char> stack = new();
for (int i = 0; i < s.Length; i++)
{
if (stack.Count == 0)
{
stack.Push(s[i]);
continue;
}
if ((s[i] == 'B' && stack.Peek() == 'A') || (s[i] == 'D' && stack.Peek() == 'C'))
{
stack.Pop();
}
else
{
stack.Push(s[i]);
}
}
return stack.Count;
}
}