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//')' { if (ST.Count > 0) { var c = ST.Peek(); if (c.Equals('(')) { ST.Pop(); } else { ST.Push(s); } } else { ST.Push(s); } } } int count = ST.Count; while (ST.Any()) { var st = ST.Pop(); } return count; } }