You are viewing a single comment's thread. Return to all comments →
Trying to be compact:
public static string isBalanced(string s) { Stack<char> st = new(); foreach (char chr in s.ToArray()) { if (new char[]{'(','[','{'}.Contains(chr)) st.Push(chr); else if ( st.Count > 0 && (int)chr > (int)st.Peek() && (int)chr-2 <= (int)st.Pop()) continue; else return "NO"; } return (st.Count > 0)?"NO":"YES"; }
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 →
Trying to be compact: