using System.Collections.Generic;
public static void Main()
string s = "lee(t(c)o)de)";
string expected = "lee(t(c)o)de";
string result = MinRemoveToMakeValidBest(s);
Console.WriteLine("result: {0}", result);
Console.WriteLine("expected: {0}", expected);
public static string MinRemoveToMakeValidBest(string s) {
var parens = new Stack<int>();
var sb = new StringBuilder();
bool[] invalid = new bool[s.Length];
for (int i = 0; i < s.Length; i++)
invalid[parens.Pop()] = true;
for (int i = 0; i < s.Length; i++)
public static string MinRemoveToMakeValid(string s) {
Stack parens = new Stack();
for (int i = 0; i < s.Length; i++)
if (parens.Pop() == null) {
for (int i = s.Length - 1; i >= 0; i--)
if (parens.Pop() == null) {
ListNode temp = head.next;
head.next = head.next.next;
public void Push(char val)
ListNode newNode = new ListNode();
newNode.next = head.next;
return head.next == null;