update BalancedBrackets

This commit is contained in:
shellhub
2019-10-12 20:48:38 +08:00
parent 9fdb1c85f7
commit 8ae6b4e64b

View File

@ -1,10 +1,8 @@
package data_structures.Stacks; package DataStructures.Stacks;
import java.util.Scanner;
import java.util.Stack; import java.util.Stack;
/** /**
*
* The nested brackets problem is a problem that determines if a sequence of * The nested brackets problem is a problem that determines if a sequence of
* brackets are properly nested. A sequence of brackets s is considered properly * brackets are properly nested. A sequence of brackets s is considered properly
* nested if any of the following conditions are true: - s is empty - s has the * nested if any of the following conditions are true: - s is empty - s has the
@ -15,71 +13,69 @@ import java.util.Stack;
* returns true if S is nested and false otherwise. * returns true if S is nested and false otherwise.
* *
* @author akshay sharma * @author akshay sharma
* @date: 2017-10-17
* @author <a href="https://github.com/khalil2535">khalil2535<a> * @author <a href="https://github.com/khalil2535">khalil2535<a>
* * @author shellhub
*/ */
class BalancedBrackets { class BalancedBrackets {
/** /**
* Check if {@code leftBracket} and {@code rightBracket} is paired or not
* *
* @param s * @param leftBracket left bracket
* @return * @param rightBracket right bracket
* @return {@code true} if {@code leftBracket} and {@code rightBracket} is paired,
* otherwise {@code false}
*/ */
static boolean is_balanced(String s) { public static boolean isPaired(char leftBracket, char rightBracket) {
Stack<Character> bracketsStack = new Stack<>(); char[][] pairedBrackets = {
char[] text = s.toCharArray(); {'(', ')'},
for (char x : text) { {'[', ']'},
switch (x) { {'{', '}'},
case '{': {'<', '>'}
case '<': };
case '(': for (char[] pairedBracket : pairedBrackets) {
case '[': if (pairedBracket[0] == leftBracket && pairedBracket[1] == rightBracket) {
bracketsStack.push(x); return true;
break;
case '}':
if (!bracketsStack.empty() && bracketsStack.pop() == '{') {
break;
} else {
return false;
}
case '>':
if (!bracketsStack.empty() && bracketsStack.pop() == '<') {
break;
} else {
return false;
}
case ')':
if (!bracketsStack.empty() && bracketsStack.pop() == '(') {
break;
} else {
return false;
}
case ']':
if (!bracketsStack.empty() && bracketsStack.pop() == '[') {
break;
} else {
return false;
}
} }
} }
return bracketsStack.empty(); return false;
} }
/** /**
* Check if {@code brackets} is balanced
* *
* @param args * @param brackets the brackets
* @TODO remove main method and Test using JUnit or other methodology * @return {@code true} if {@code brackets} is balanced, otherwise {@code false}
*/ */
public static void main(String args[]) { public static boolean isBalanced(String brackets) {
try (Scanner in = new Scanner(System.in)) { if (brackets == null) {
System.out.println("Enter sequence of brackets: "); throw new IllegalArgumentException("brackets is null");
String s = in.nextLine(); }
if (is_balanced(s)) { Stack<Character> bracketsStack = new Stack<>();
System.out.println(s + " is balanced"); for (char bracket : brackets.toCharArray()) {
} else { switch (bracket) {
System.out.println(s + " ain't balanced"); case '(':
case '[':
case '{':
bracketsStack.push(bracket);
break;
case ')':
case ']':
case '}':
if (!(!bracketsStack.isEmpty() && isPaired(bracketsStack.pop(), bracket))) {
return false;
}
break;
default: /* other character is invalid */
return false;
} }
} }
return bracketsStack.isEmpty();
}
public static void main(String[] args) {
assert isBalanced("[()]{}{[()()]()}");
assert !isBalanced("[(])");
} }
} }