From 87682d373ac3165f567857c986968e17c3301dba Mon Sep 17 00:00:00 2001 From: Nowele Rechka Date: Fri, 25 Sep 2020 21:56:48 -0600 Subject: [PATCH] #46 - isOpen --- Interview_Questions/BalanceParentheses.java | 39 ++++++++++++++++----- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/Interview_Questions/BalanceParentheses.java b/Interview_Questions/BalanceParentheses.java index a1b100c..958bc70 100644 --- a/Interview_Questions/BalanceParentheses.java +++ b/Interview_Questions/BalanceParentheses.java @@ -25,21 +25,25 @@ public class BalanceParentheses { - public static void balanceParentheses(String brackets) { + // Characters + static char curlyOpen = '{'; + static char curlyClose = '}'; + static char bracketOpen = '['; + static char bracketClose = ']'; + static char parenthesesOpen = '('; + static char parenthesesClose = ')'; - // Characters - String curlyOpen = "{"; - String curlyClose = "}"; - String bracketOpen = "["; - String bracketClose = "]"; - String parenthesesOpen = "("; - String parenthesesClose = ")"; + public static void balanceParentheses(String brackets) { String result = ""; - ArrayList balanceList = new ArrayList(); + String balanceString = ""; for (int i = 0; i <= brackets.length()-1; i++) { // if bracket is an open character, add it to the balanceList + char c = brackets.charAt(i); + if (isOpen(c)) { + balanceString += c; + } //if a char is a close character and it is match for the last character in the balanceList, pop the open character. @@ -53,6 +57,23 @@ public static void balanceParentheses(String brackets) { System.out.println("result: " + result); } + public static Boolean isOpen(char c) { + if (c == curlyOpen || c == bracketOpen || c == parenthesesOpen) { + return true; + } + else { + return false; + } + } + + public static String yes() { + return "Yes"; + } + + public static String no() { + return "No"; + } + public static void main(String[] args) { balanceParentheses("(())"); // Yes balanceParentheses("(())"); // Yes