using System.Runtime.InteropServices;
using System.Collections.Generic;
using System.Text.RegularExpressions;
public static void Main()
string testString1 = "{[balanced(parenthesis)]}";
string testString2 = "(test)[wrong bracket type)";
string testString3 = "(test)[Mismatched]((sdff)";
bool isValid1 = ValidateString(testString1);
bool isValid2 = ValidateString(testString2);
bool isValid3 = ValidateString(testString3);
Console.WriteLine("TestString1 is balanced correctly!");
else Console.WriteLine("TestString1 is NOT balanced properly!");
Console.WriteLine("TestString2 is balanced correctly!");
else Console.WriteLine("TestString2 is NOT balanced properly!");
Console.WriteLine("TestString3 is balanced correctly!");
else Console.WriteLine("TestString3 is NOT balanced properly!");
public static bool ValidateString(string testString)
var lastOpener = new Stack<char>();
foreach (char c in testString)
if (c == ')' && lastOpener.Pop() == '(')
if (c == ']' && lastOpener.Pop() == '[')
if (c == '}' && lastOpener.Pop() == '{')
} catch { return false; }
if (p1 != 0 || p2 != 0 || p3 != 0)