Program to check valid parenthesis
public class Solution { public bool IsValid(string s) { Stack stack=new Stack(); foreach(char ch in s) { if(ch=='(' || ch=='{' || ch=='[') stack.Push(ch); if(ch==')' || ch=='}' || ch==']') stack.Pop(); } if(stack.Count==0) return true; else return false; } }
Leave a Reply