c# First practice in 2020
January 8, 2020\n1249. Minimum Remove to Make Valid Parentheses\n\nI took a more than three weeks break and it is the first algorithm I like to solve in 2020. I
January 8, 2020\n1249. Minimum Remove to Make Valid Parentheses\n\nI took a more than three weeks break and it is the first algorithm I like to solve in 2020. I like to solve 240 algorithms in 2020. This is my first one. \n\n**Case study**\nIn order for me to solve the algorithm, I learn that I have to put two test cases into consideration. \nCase 1: lee(t(c)o)de)\nThe idea is to use stack to store all indexes of open paretheses, and if there is \')\', then remove matched open parethese. Otherwise replace stringbuilder temporary variable **copy** - a copy of string - using placeholder char * at index of \')\'. We can name it "replace unmatched close parenthese with wildchar *". \n\nlee(t(c)o)de) -> lee(t(c)o)de* -> remove * in the string. \nput \'(\' in stack, and if there is matched \')\', then pop \'(\' otherwise replace stringBuilder index position with \'*\'.\n\nCase 2: ))((\nDo not forget to pop out all chars in the stack, remove open parenthese \'(\' at the index position in **copy** variable. \n\nHere are my C# practice highlights:\n1. C# string is immutable, so declare a StringBuilder variable to make a copy of string. \n2. Get familiar with C# StringBuilder.Replace and Remove API, empty char has issue to use Replace, so empty string "" works. \n3. Always work on my weakness, come out test cases to fully test the code by myself first. This time I missed test case ))((. I quickly fixed the bug after online judge showed me the bug with the test case ))((.  \n\n```\nusing System;\nusing System.Collections.Generic;\nusing System.Linq;\nusing System.Text;\nusing System.Threading.Tasks;\n\nnamespace _1249_minimumToRemove\n{\n    class Program\n    {\n        static void Main(string[] args)\n        {\n            var result = MinRemoveToMakeValid("lee(t(c)o)d)e"); \n        }\n\n        /// <summary>\n        /// lee(t(c)o)de) -> lee(t(c)o)de* -> remove * in the string\n        /// put ( in stack, and if there is matched ), then pop ( otherwise replace stringBuilder index position with *\n        /// </summary>\n        /// <param name="s"></param>\n        /// <returns></returns>\n        public static String MinRemoveToMakeValid(String s)\n        {\n            var sb = new StringBuilder(s);\n            var stack = new Stack<int>();\n\n            var length = s.Length;\n            for (int i = 0; i < length; i++)\n            {\n                var current = s[i];\n                var isOpen = current == \'(\';\n                var isClose = current == \')\';\n\n                if (isOpen)\n                {\n                    stack.Push(i); \n                }\n\n                if (isClose)\n                {\n                    if (stack.Count > 0)\n                    {\n                        stack.Pop();\n                    }\n                    else\n                    {\n                        sb.Replace(\')\',\'*\',i,1);\n                    }\n                }\n            }\n\n            // remove extract ((, for example ))((\n            while (stack.Count > 0)\n            {                \n                sb.Remove(stack.Pop(), 1); \n            }\n\n            return sb.Replace("*","",0,sb.Length).ToString();\n        }\n    }\n}\n```
[]