BAEL-3481 - updated with review comments.

This commit is contained in:
BudBak 2020-01-04 16:28:52 +05:30
parent ba7ac3c5b7
commit 8676327794
2 changed files with 23 additions and 22 deletions

View File

@ -1,8 +1,9 @@
package com.baeldung.algorithms.balancedbrackets;
import java.util.Stack;
import java.util.Deque;
import java.util.LinkedList;
public class BalancedBracketsUsingStack {
public class BalancedBracketsUsingDeque {
public boolean isBalanced(String str) {
if (null == str || str.length() == 0 || ((str.length() % 2) != 0)) {
@ -17,17 +18,17 @@ public class BalancedBracketsUsingStack {
}
}
Stack<Character> stack = new Stack<>();
Deque<Character> deque = new LinkedList<>();
for (char ch: str.toCharArray()) {
if (ch == '{' || ch == '[' || ch == '(') {
stack.push(ch);
deque.addFirst(ch);
} else {
if ( !stack.isEmpty()
&& ((stack.peek() == '{' && ch == '}')
|| (stack.peek() == '[' && ch == ']')
|| (stack.peek() == '(' && ch == ')')
if ( !deque.isEmpty()
&& ((deque.peekFirst() == '{' && ch == '}')
|| (deque.peekFirst() == '[' && ch == ']')
|| (deque.peekFirst() == '(' && ch == ')')
)) {
stack.pop();
deque.removeFirst();
} else {
return false;
}

View File

@ -5,75 +5,75 @@ import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class BalancedBracketsUsingStackUnitTest {
private BalancedBracketsUsingStack balancedBracketsUsingStack;
public class BalancedBracketsUsingDequeUnitTest {
private BalancedBracketsUsingDeque balancedBracketsUsingDeque;
@Before
public void setup() {
balancedBracketsUsingStack = new BalancedBracketsUsingStack();
balancedBracketsUsingDeque = new BalancedBracketsUsingDeque();
}
@Test
public void givenNullInput_whenCheckingForBalance_shouldReturnFalse() {
boolean result = balancedBracketsUsingStack.isBalanced(null);
boolean result = balancedBracketsUsingDeque.isBalanced(null);
assertThat(result).isFalse();
}
@Test
public void givenEmptyString_whenCheckingForBalance_shouldReturnFalse() {
boolean result = balancedBracketsUsingStack.isBalanced("");
boolean result = balancedBracketsUsingDeque.isBalanced("");
assertThat(result).isFalse();
}
@Test
public void givenInvalidCharacterString_whenCheckingForBalance_shouldReturnFalse() {
boolean result = balancedBracketsUsingStack.isBalanced("abc[](){}");
boolean result = balancedBracketsUsingDeque.isBalanced("abc[](){}");
assertThat(result).isFalse();
}
@Test
public void givenOddLengthString_whenCheckingForBalance_shouldReturnFalse() {
boolean result = balancedBracketsUsingStack.isBalanced("{{[]()}}}");
boolean result = balancedBracketsUsingDeque.isBalanced("{{[]()}}}");
assertThat(result).isFalse();
}
@Test
public void givenEvenLengthString_whenCheckingForBalance_shouldReturnFalse() {
boolean result = balancedBracketsUsingStack.isBalanced("{{[]()}}}}");
boolean result = balancedBracketsUsingDeque.isBalanced("{{[]()}}}}");
assertThat(result).isFalse();
}
@Test
public void givenEvenLengthUnbalancedString_whenCheckingForBalance_shouldReturnFalse() {
boolean result = balancedBracketsUsingStack.isBalanced("{[(])}");
boolean result = balancedBracketsUsingDeque.isBalanced("{[(])}");
assertThat(result).isFalse();
}
@Test
public void givenEvenLengthBalancedString_whenCheckingForBalance_shouldReturnTrue() {
boolean result = balancedBracketsUsingStack.isBalanced("{[()]}");
boolean result = balancedBracketsUsingDeque.isBalanced("{[()]}");
assertThat(result).isTrue();
}
@Test
public void givenBalancedString_whenCheckingForBalance_shouldReturnTrue() {
boolean result = balancedBracketsUsingStack.isBalanced("{{[[(())]]}}");
boolean result = balancedBracketsUsingDeque.isBalanced("{{[[(())]]}}");
assertThat(result).isTrue();
}
@Test
public void givenAnotherBalancedString_whenCheckingForBalance_shouldReturnTrue() {
boolean result = balancedBracketsUsingStack.isBalanced("{{([])}}");
boolean result = balancedBracketsUsingDeque.isBalanced("{{([])}}");
assertThat(result).isTrue();
}
@Test
public void givenUnBalancedString_whenCheckingForBalance_shouldReturnFalse() {
boolean result = balancedBracketsUsingStack.isBalanced("{{)[](}}");
boolean result = balancedBracketsUsingDeque.isBalanced("{{)[](}}");
assertThat(result).isFalse();
}