Algorithm for matching parentheses pairs in C#