Skip to content

Commit e1c4fe0

Browse files
authored
Create yeonguchoe.cs
1 parent 2f687ad commit e1c4fe0

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

valid-parentheses/yeonguchoe.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)