Given a string S of '(' and ')' parentheses, we add the minimum number of parentheses ( '(' or ')', and in any positions ) so that the resulting parentheses string is valid. Formally, a parentheses string is valid if and only if: It is the empty stri…
public class Solution { public int MinAddToMakeValid(string S) { Stack<char> ST = new Stack<char>(); foreach (var s in S) { if (s.Equals('(')) { ST.Push(s); } else//')' { ) { var c = ST.Peek(); if (c.Equals('(')) { ST.Pop(); } else { ST.Push(s…