You are viewing a single comment's thread. Return to all comments →
C#
public static string isBalanced(string s) { Stack<char> left = new Stack<char>(); int lastLeftIndex = 0; for(int i = 0; i < s.Length; i++) { if(s[i] == '(' || s[i] == '{' || s[i] == '[') { left.Push(s[i]); } else if(s[i] == ')' && (left.Count == 0 || left.Pop() != '(')) { return "NO"; } else if(s[i] == '}' && (left.Count == 0 ||left.Pop() != '{')) { return "NO"; } else if(s[i] == ']' && (left.Count == 0 ||left.Pop() != '[')) { return "NO"; } } if(left.Count > 0) { return "NO"; } return "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 →
C#