A Programming Job Interview Challenge #13 Follow up

Under repeated advisement of my work colleage he really wished to see me implement my psuedo code precisely. This made me realize there was one ambigiouity to my psuedo code was stack.Push should only be called if the character is NOT a matching close character. Now onto the code.

public void LanguageTest()
{
Hashtable language = new Hashtable();
language.Add(‘)’, ‘(‘);
language.Add(‘}’, ‘{‘);
language.Add(‘>’, ‘<‘);
language.Add(‘]’, ‘[‘);

bool valid = IsValidExpression(language, “([](<{}>))”);
valid = IsValidExpression(language, “({<)>}”);
}

private static bool IsValidExpression(IDictionary language, IEnumerable<char> expr)
{
Stack stack = new Stack();
foreach (char c in expr)
{
if (language.Contains(c))
{
if ((char) language[c] != (char) stack.Pop())
return false;
}
else
{
stack.Push(c);
}
}

return true;
}

BloggingContext.ApplicationInstance.CompleteRequest();