using System.Collections.Generic;
static Dictionary<string, string> Match = new Dictionary<string, string>()
{{"<", ">"}, {"{", "}"}, {"(", ")"}, {"[", "]"}, };
public static void Main()
var proper = "{ab(de[ef<gh}}ijk}lmn}";
var error = "{ab(de[ef<gh>)]ijk)lmn}";
Console.WriteLine(proper + " is matching: " + FindMatching(proper, 0, 0));
static bool FindMatching(string str)
Stack<string> stack = new();
for (var i = 0; i < str.Length; i++)
var text = str[i].ToString();
if (Match.ContainsKey(text))
if (Match.ContainsValue(text))
string closing = stack.Pop();
static bool FindMatching(string str, int open, int close)
if (String.IsNullOrEmpty(str))
string text = str[0].ToString();
if (Match.ContainsKey(text))
return FindMatching(str.Substring(1), open + 1, close);
else if (Match.ContainsValue(text))
return FindMatching(str.Substring(1), open, close + 1);
return FindMatching(str.Substring(1), open, close);