Check for balanced parentheses I was given an assignment to check for balanced parentheses from an arbitrary string where parentheses are defined as (, [ or { and their respective "closing" parentheses. The other requirement for the assignment was that I had to use either Stack&lt;T&gt; , Queue&lt;T&gt; or List&lt;T&gt;. 

Here is my implementation:

private static void CheckParentheses()
{
    Console.Write("Please enter the string you wist to check formity of: ");
    var input = Console.ReadLine();
    var stack = new Stack&lt;char&gt;();

    char[] allowedChars = { '(', '[', '{', ')', ']', '}' };

    foreach (char chr in input)
        if (allowedChars.Any(x =&gt; x == chr))
            stack.Push(chr);

    var reverseStack = stack.Reverse();
    var sequencedBalanced = reverseStack.SequenceEqual(stack, BalancedParanthesisComparer.Instance);
    Console.WriteLine("The input string was {0}well formed", sequencedBalanced ? string.Empty : "NOT ");
}

public sealed class BalancedParanthesisComparer : EqualityComparer&lt;char&gt;
{
    private static readonly BalancedParanthesisComparer _instance = new BalancedParanthesisComparer();

    private BalancedParanthesisComparer() { }

    public static BalancedParanthesisComparer Instance { get { return _instance; } }

    public override bool Equals(char x, char y)
    {
        if ((x == '(' &amp;&amp; y == ')') || (y == '(' &amp;&amp; x == ')'))
            return true;
        if ((x == '[' &amp;&amp; y == ']') || (y == '[' &amp;&amp; x == ']'))
            return true;
        if (x == '{' &amp;&amp; y == '}' || (y == '{' &amp;&amp; x == '}'))
            return true;

        return false;
    }
}


I would like general feedback and would also like to know: regarding the use of an EqualityComparer to do something that isn't really equality-checking, is it very bad practice or is it fine?
Aside from stylistic concerns (as a Java/Clojure guy, I'll let you C#'ers decide on that stuff), I don't see how this a correct algorithm, unless additional constraints are given in the problem statement. Consider this input:

([] ())

Now, clearly that is balanced. And if you reverse it, you get:

))( ][(

But if you compare this reversed input with the original input, you don't find that each open bracket matches up with its closing bracket in the reversed string: the first character and the last are fine, but the inner pairs don't balance at all!

So, unless you are given the further guarantee that the input consists of N opening brackets followed by N closing brackets (ie, no opens after the first close), you can't just compare the reversed string.