using System.Collections.Generic;
public static void Main()
var strTest1 = "{1/[5/(1+2)+2*(3^5-1) +4] *(9+3)}";
var strTest2 = "{{{}}()}";
bool res = CheckParens(strTest2);
private static bool CheckParens(string str)
Func<string, int, int> checkRec = null;
if (IsClosedParen(s[idx]))
idx = checkRec(s, idx + 1);
if (idx < 0 || idx == s.Length || !ParensMatch(openParen, s[idx]))
int i = checkRec(str, 0);
if (i < 0 || i < str.Length)
private static bool ParensMatch(char pOpen, char pClosed)
return parensMatcher[pOpen] == pClosed;
private static bool IsOpenParen(char c)
return "([{".IndexOf(c) != -1;
private static bool IsClosedParen(char c)
return ")]}".IndexOf(c) != -1;
private static Dictionary<char, char> parensMatcher = new Dictionary<char, char>(){ {'(', ')'}, { '[', ']' }, { '{', '}' } };