We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
staticstringisBalanced(strings){// Complete this functionif(s.Length%2==1)return"NO";Char[]bc=s.ToCharArray();int[]b=newint[bc.Length];for(inti=0;i<b.Length;i++)b[i]=Convert.ToInt32(bc[i]);Stack<int>sta=newStack<int>();intc;for(inti=0;i<b.Length;i++){if(sta.Count==0){if(b[i]==125||b[i]==93||b[i]==41)return"NO";sta.Push(b[i]);continue;}c=sta.Peek();switch(b[i]){case125:case93:if(c!=b[i]-2)return"NO";sta.Pop();break;case41:if(c!=40)return"NO";sta.Pop();break;default:sta.Push(b[i]);break;}}return(sta.Count>0)?"NO":"YES";}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Balanced Brackets
You are viewing a single comment's thread. Return to all comments →
c#: