Thanks to visit codestin.com Credit goes to github.com
We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 1ceb60f commit 5bec4b6Copy full SHA for 5bec4b6
csharp/20-Valid-Parentheses.cs
@@ -0,0 +1,35 @@
1
+using System;
2
+namespace AlgoPractice
3
+{
4
+ public class Solution
5
+ {
6
+ public bool IsValid(string s)
7
8
+ var map = new Dictionary<char, char>
9
10
+ [')'] = '(',
11
+ ['}'] = '{',
12
+ [']'] = '['
13
+ };
14
+
15
+ var stack = new Stack<char>();
16
+ foreach(char c in s)
17
18
+ if (map.ContainsKey(c))
19
20
+ var matchingPar = stack.Count == 0 ? '#' : stack.Pop();
21
+ if(map[c] != matchingPar)
22
23
+ return false;
24
+ }
25
26
+ else
27
28
+ stack.Push(c);
29
30
31
+ return stack.Count == 0;
32
33
34
+}
35
0 commit comments