Valid Parentheses
Last updated
Last updated
public class Solution {
public bool IsValid(string s) {
if(s.Length%2!=0){
return false;
}
Stack<char> braces=new Stack<char>();
foreach( char ch in s){
if(braces.Count==0){
braces.Push(ch);
continue;
}
if(ch==')' && braces.Peek()=='('){
braces.Pop();
}
else if(ch=='}' && braces.Peek()=='{'){
braces.Pop();
}
else if(ch==']' && braces.Peek()=='['){
braces.Pop();
}
else{
braces.Push(ch);
}
}
return braces.Count==0;
}
}