Skip to content

refactor: DuplicateBrackets #5424

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 26 additions & 20 deletions src/main/java/com/thealgorithms/stacks/DuplicateBrackets.java
Original file line number Diff line number Diff line change
@@ -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<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++) {
char ch = str.charAt(i);
Stack<Character> 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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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));
}
}