Created
February 17, 2018 22:17
-
-
Save jianminchen/9a99f70b3072920f65470c7e9dd33d49 to your computer and use it in GitHub Desktop.
Feb. 17, 2018 - 2:16 PM, continue to implement and test code written in mock interview. The design has several bugs by adding left open parentheses.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace FindMinimumParentheses | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var result = GetMinimumRequireParentheses("(())"); // (()) - correct | |
var resutl2 = GetMinimumRequireParentheses(")))()"); // (() - wrong | |
var result3 = GetMinimumRequireParentheses("(())"); // ()) - wrong | |
var result4 = GetMinimumRequireParentheses("((())"); // ()) - wrong | |
var result5 = GetMinimumRequireParentheses("((()))"); // (())) - wrong | |
} | |
public static string GetMinimumRequireParentheses(string text) | |
{ | |
if(text == null || text.Length == 0) | |
return ""; | |
var length = text.Length; | |
int openBracket = 0; | |
var minimumString = new StringBuilder(); | |
char previous = ' '; | |
for(int index = 0; index < length; index++) | |
{ | |
var current = text[index]; | |
var isOpen = current == '('; | |
if(isOpen) | |
{ | |
openBracket++; | |
// add ( | |
if(openBracket == 1) | |
{ | |
minimumString.Append("("); | |
} | |
} | |
else | |
{ | |
if(openBracket > 0) | |
{ | |
// add - previous char ), I have to special handling , add () | |
// missing open bracket somewhere before | |
if(previous == '(') | |
{ | |
minimumString.Append("()"); // | |
} | |
else | |
{ // 1. (()) -> no need to add - extra ) | |
// 2. ()()) -> need to add extra big parenthese -> | |
minimumString.Append(")"); // but I missed one open bracket at the beginng | |
} | |
openBracket--; | |
} | |
} | |
previous = current; | |
} | |
// edge case | |
var validString = minimumString.ToString(); | |
if(openBracket > 0) | |
{ | |
var openBracketIndex = getExtraOpenBracketIndex(minimumString.ToString()); | |
if(openBracketIndex >= 0) | |
{ | |
return openBracketIndex == 0 ? validString.Substring(1) : | |
(validString.Substring(0, openBracket) + | |
validString.Substring(openBracket + 1, validString.Length - openBracket - 1)); | |
} | |
} | |
return minimumString.ToString(); | |
} | |
private static int getExtraOpenBracketIndex(string s) | |
{ | |
if(s == null || s.Length == 0) | |
{ | |
return -1; | |
} | |
// remove the last one open bracket | |
var length = s.Length; | |
for (int i = 0; i < length; i++) | |
{ | |
var current = s[length - 1 - i]; | |
if (current == '(') | |
{ | |
return i; | |
} | |
} | |
return -1; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment