Skip to content

Commit

Permalink
akshitagit#46 - isOpen
Browse files Browse the repository at this point in the history
  • Loading branch information
Nowele committed Sep 26, 2020
1 parent 11f20b1 commit 87682d3
Showing 1 changed file with 30 additions and 9 deletions.
39 changes: 30 additions & 9 deletions Interview_Questions/BalanceParentheses.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> balanceList = new ArrayList<String>();
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.

Expand All @@ -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
Expand Down

0 comments on commit 87682d3

Please sign in to comment.