using System.Collections.Generic;
public static void Main()
bool isValid = IsValid(exp);
Console.WriteLine(isValid);
static bool IsValid(string exp)
if (string.IsNullOrEmpty(exp))
var matches = new Dictionary<char, char>
while (exp != string.Empty)
for (int i = 0; i < exp.Length; i++)
if (matches.ContainsKey(curr))
var prevMatch = matches[curr];
exp = exp.Remove(i - 1, 2);