From 8676327794991ed6e36bfc4b73fa35de6a690b75 Mon Sep 17 00:00:00 2001 From: BudBak Date: Sat, 4 Jan 2020 16:28:52 +0530 Subject: [PATCH] BAEL-3481 - updated with review comments. --- ...k.java => BalancedBracketsUsingDeque.java} | 19 +++++++------- ...> BalancedBracketsUsingDequeUnitTest.java} | 26 +++++++++---------- 2 files changed, 23 insertions(+), 22 deletions(-) rename algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/balancedbrackets/{BalancedBracketsUsingStack.java => BalancedBracketsUsingDeque.java} (63%) rename algorithms-miscellaneous-5/src/test/java/com/baeldung/algorithms/balancedbrackets/{BalancedBracketsUsingStackUnitTest.java => BalancedBracketsUsingDequeUnitTest.java} (70%) diff --git a/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/balancedbrackets/BalancedBracketsUsingStack.java b/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/balancedbrackets/BalancedBracketsUsingDeque.java similarity index 63% rename from algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/balancedbrackets/BalancedBracketsUsingStack.java rename to algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/balancedbrackets/BalancedBracketsUsingDeque.java index 9e4592a512..2734a622e3 100644 --- a/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/balancedbrackets/BalancedBracketsUsingStack.java +++ b/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/balancedbrackets/BalancedBracketsUsingDeque.java @@ -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 stack = new Stack<>(); + Deque 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; } diff --git a/algorithms-miscellaneous-5/src/test/java/com/baeldung/algorithms/balancedbrackets/BalancedBracketsUsingStackUnitTest.java b/algorithms-miscellaneous-5/src/test/java/com/baeldung/algorithms/balancedbrackets/BalancedBracketsUsingDequeUnitTest.java similarity index 70% rename from algorithms-miscellaneous-5/src/test/java/com/baeldung/algorithms/balancedbrackets/BalancedBracketsUsingStackUnitTest.java rename to algorithms-miscellaneous-5/src/test/java/com/baeldung/algorithms/balancedbrackets/BalancedBracketsUsingDequeUnitTest.java index 11b246658c..1d791dd29e 100644 --- a/algorithms-miscellaneous-5/src/test/java/com/baeldung/algorithms/balancedbrackets/BalancedBracketsUsingStackUnitTest.java +++ b/algorithms-miscellaneous-5/src/test/java/com/baeldung/algorithms/balancedbrackets/BalancedBracketsUsingDequeUnitTest.java @@ -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(); }