From c57e02dc856e61118d89609f34900ff6d4d0376c Mon Sep 17 00:00:00 2001 From: Alex Klymenko Date: Thu, 29 Aug 2024 15:51:05 +0200 Subject: [PATCH] refactor: `DuplicateBrackets` (#5424) refactor: DuplicateBrackets Co-authored-by: alxkm --- .../stacks/DuplicateBrackets.java | 46 +++++++++++-------- .../stacks/DuplicateBracketsTest.java | 29 ++++++++++++ 2 files changed, 55 insertions(+), 20 deletions(-) create mode 100644 src/test/java/com/thealgorithms/stacks/DuplicateBracketsTest.java diff --git a/src/main/java/com/thealgorithms/stacks/DuplicateBrackets.java b/src/main/java/com/thealgorithms/stacks/DuplicateBrackets.java index 482bda0a..25d26523 100644 --- a/src/main/java/com/thealgorithms/stacks/DuplicateBrackets.java +++ b/src/main/java/com/thealgorithms/stacks/DuplicateBrackets.java @@ -1,37 +1,43 @@ package com.thealgorithms.stacks; -// 1. You are given a string exp representing an expression. -// 2. Assume that the expression is balanced i.e. the opening and closing brackets match with each -// other. -// 3. But, some of the pair of brackets maybe extra/needless. -// 4. You are required to print true if you detect extra brackets and false otherwise. -// e.g.' -// ((a + b) + (c + d)) -> false -// (a + b) + ((c + d)) -> true import java.util.Stack; +/** + * Class for detecting unnecessary or redundant brackets in a mathematical expression. + * Assumes the expression is balanced (i.e., all opening brackets have matching closing brackets). + */ public final class DuplicateBrackets { private DuplicateBrackets() { } - public static boolean check(String str) { - Stack st = new Stack<>(); + /** + * Checks for extra or redundant brackets in a given expression. + * + * @param expression the string representing the expression to be checked + * @return true if there are extra or redundant brackets, false otherwise + * @throws IllegalArgumentException if the input string is null + */ + public static boolean check(String expression) { + if (expression == null) { + throw new IllegalArgumentException("Input expression cannot be null."); + } - for (int i = 0; i < str.length(); i++) { - char ch = str.charAt(i); + Stack stack = new Stack<>(); + for (int i = 0; i < expression.length(); i++) { + char ch = expression.charAt(i); if (ch == ')') { - if (st.peek() == '(') { + if (stack.isEmpty() || stack.peek() == '(') { return true; - } else { - while (st.size() > 0 && st.peek() != '(') { - st.pop(); - } - st.pop(); + } + while (!stack.isEmpty() && stack.peek() != '(') { + stack.pop(); + } + if (!stack.isEmpty()) { + stack.pop(); } } else { - st.push(ch); + stack.push(ch); } - // System.out.println(st); } return false; } diff --git a/src/test/java/com/thealgorithms/stacks/DuplicateBracketsTest.java b/src/test/java/com/thealgorithms/stacks/DuplicateBracketsTest.java new file mode 100644 index 00000000..e2cc6acb --- /dev/null +++ b/src/test/java/com/thealgorithms/stacks/DuplicateBracketsTest.java @@ -0,0 +1,29 @@ +package com.thealgorithms.stacks; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; + +class DuplicateBracketsTest { + + @ParameterizedTest + @CsvSource({"'((a + b) + (c + d))'", "'(a + b)'", "'a + b'", "'('", "''"}) + void testInputReturnsFalse(String input) { + assertFalse(DuplicateBrackets.check(input)); + } + + @ParameterizedTest + @CsvSource({"'(a + b) + ((c + d))'", "'((a + b))'", "'((((a + b)))))'"}) + void testInputReturnsTrue(String input) { + assertTrue(DuplicateBrackets.check(input)); + } + + @Test + void testInvalidInput() { + assertThrows(IllegalArgumentException.class, () -> DuplicateBrackets.check(null)); + } +}