https://leetcode.com/problems/valid-parentheses/
괄호로 이루어진 문자열이 주어졌을 때 올바른 것인지 아닌지 반환
public class Solution {
public bool IsValid(string s) {
Stack<char> stack = new Stack<char>();
Dictionary<char, char> pairDict = new Dictionary<char, char>();
pairDict.Add(')', '(');
pairDict.Add('}', '{');
pairDict.Add(']', '[');
foreach(char p in s)
{
if (p == '(' || p == '{' || p == '[')
{
stack.Push(p);
}
else
{
if (stack.Count == 0) return false;
if (stack.Peek() != pairDict[p]) return false;
stack.Pop();
}
}
return (stack.Count == 0);
}
}