Adding matching parentheses problem-algorithm