I am reading the Algorithm Design Manual Second Edition and this is from an exercise question. Quoting the question
A common problem for comp
Piggy back on @Russell's idea:
public class BalancedBrackets
{
private readonly char[] _leftBrackets = new char[] {'[', '(', '{', '<'};
private readonly char[] _rightBrackets = new char[] {']', ')', '}', '>'};
public bool IsBalanced(string input)
{
int count = 0;
foreach (var character in input.ToCharArray())
{
if (_leftBrackets.Contains(character)) count++;
if (_rightBrackets.Contains(character)) count--;
}
return count == 0;
}
}