diff --git a/solution/0000-0099/0020.Valid Parentheses/solutions.cs b/solution/0000-0099/0020.Valid Parentheses/solutions.cs new file mode 100644 index 0000000000000..f0b79f30fbc19 --- /dev/null +++ b/solution/0000-0099/0020.Valid Parentheses/solutions.cs @@ -0,0 +1,17 @@ +public class Solution { + public bool IsValid(string s) { + Stack stk = new Stack(); + foreach (var c in s.ToCharArray()) { + if (c == '(') { + stk.Push(')'); + } else if (c == '[') { + stk.Push(']'); + } else if (c == '{') { + stk.Push('}'); + } else if (stk.Count == 0 || stk.Pop() != c) { + return false; + } + } + return stk.Count == 0; + } +}