public bool?Check(BracketList bracketList, out bool isDetermined) { bool?isBalanced = null; int count = 0; //algorithm for (int j = 0; j < bracketList.List.Count; j++) { //check for too many right brackets if (bracketList.List[j] == bracketList.LeftBracket) { count++; } else if (bracketList.List[j] == bracketList.RightBracket) { count--; } //check for equal left and right brackets if (count < 0) { isBalanced = false; isDetermined = true; break; } } //if it is unbalanced, the answer for the input has been determined if (count != 0) { isBalanced = false; isDetermined = true; } else { isDetermined = false; } return(isBalanced); }
private List <BracketList> SplitRawInput() { allBrackets = new char[rawInput.Length]; for (int i = 0; i < rawInput.Length; i++) { allBrackets[i] = rawInput[i]; } squares = new BracketList(allBrackets.Where(el => el.Equals('[') || el.Equals(']')).ToList(), '[', ']'); curlies = new BracketList(allBrackets.Where(el => el.Equals('{') || el.Equals('}')).ToList(), '{', '}'); parentheses = new BracketList(allBrackets.Where(el => el.Equals('(') || el.Equals(')')).ToList(), '(', ')'); List <BracketList> bracketLists = new List <BracketList>(3) { squares , curlies , parentheses }; //sorting this by count gives performance gains for later iterations bracketLists.OrderBy(el => el.List.Count); return(bracketLists); }