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 2f687ad commit e1c4fe0Copy full SHA for e1c4fe0
valid-parentheses/yeonguchoe.cs
@@ -0,0 +1,24 @@
1
+public class Solution {
2
+ public bool IsValid(string s) {
3
+ Stack<char> parentheses = new Stack<char>();
4
+
5
+ Dictionary<char, char> pair = new Dictionary<char, char> {
6
+ { ')', '(' },
7
+ { '}', '{' },
8
+ { ']', '[' }
9
+ };
10
11
+ foreach (char c in s) {
12
+ if (c == '(' || c == '{' || c == '[') {
13
+ parentheses.Push(c);
14
+ }
15
+ else if (c == ')' || c == '}' || c == ']') {
16
+ if (parentheses.Count == 0 || parentheses.Peek() != pair[c]) {
17
+ return false;
18
19
+ parentheses.Pop();
20
21
22
23
+ return parentheses.Count == 0;
24
0 commit comments