refactor: DuplicateBrackets (#5424)

refactor: DuplicateBrackets

Co-authored-by: alxkm <alx@alx.com>
This commit is contained in:
Alex Klymenko 2024-08-29 15:51:05 +02:00 committed by GitHub
parent e2aaefebd5
commit c57e02dc85
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 55 additions and 20 deletions

View File

@ -1,37 +1,43 @@
package com.thealgorithms.stacks; 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; 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 { public final class DuplicateBrackets {
private DuplicateBrackets() { private DuplicateBrackets() {
} }
public static boolean check(String str) { /**
Stack<Character> 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++) { Stack<Character> stack = new Stack<>();
char ch = str.charAt(i); for (int i = 0; i < expression.length(); i++) {
char ch = expression.charAt(i);
if (ch == ')') { if (ch == ')') {
if (st.peek() == '(') { if (stack.isEmpty() || stack.peek() == '(') {
return true; return true;
} else { }
while (st.size() > 0 && st.peek() != '(') { while (!stack.isEmpty() && stack.peek() != '(') {
st.pop(); stack.pop();
} }
st.pop(); if (!stack.isEmpty()) {
stack.pop();
} }
} else { } else {
st.push(ch); stack.push(ch);
} }
// System.out.println(st);
} }
return false; return false;
} }

View File

@ -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));
}
}