Skip to content

Commit c57e02d

Browse files
alxkmalxkm
and
alxkm
authored
refactor: DuplicateBrackets (#5424)
refactor: DuplicateBrackets Co-authored-by: alxkm <[email protected]>
1 parent e2aaefe commit c57e02d

File tree

2 files changed

+55
-20
lines changed

2 files changed

+55
-20
lines changed

src/main/java/com/thealgorithms/stacks/DuplicateBrackets.java

+26-20
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,43 @@
11
package com.thealgorithms.stacks;
22

3-
// 1. You are given a string exp representing an expression.
4-
// 2. Assume that the expression is balanced i.e. the opening and closing brackets match with each
5-
// other.
6-
// 3. But, some of the pair of brackets maybe extra/needless.
7-
// 4. You are required to print true if you detect extra brackets and false otherwise.
8-
// e.g.'
9-
// ((a + b) + (c + d)) -> false
10-
// (a + b) + ((c + d)) -> true
113
import java.util.Stack;
124

5+
/**
6+
* Class for detecting unnecessary or redundant brackets in a mathematical expression.
7+
* Assumes the expression is balanced (i.e., all opening brackets have matching closing brackets).
8+
*/
139
public final class DuplicateBrackets {
1410
private DuplicateBrackets() {
1511
}
1612

17-
public static boolean check(String str) {
18-
Stack<Character> st = new Stack<>();
13+
/**
14+
* Checks for extra or redundant brackets in a given expression.
15+
*
16+
* @param expression the string representing the expression to be checked
17+
* @return true if there are extra or redundant brackets, false otherwise
18+
* @throws IllegalArgumentException if the input string is null
19+
*/
20+
public static boolean check(String expression) {
21+
if (expression == null) {
22+
throw new IllegalArgumentException("Input expression cannot be null.");
23+
}
1924

20-
for (int i = 0; i < str.length(); i++) {
21-
char ch = str.charAt(i);
25+
Stack<Character> stack = new Stack<>();
26+
for (int i = 0; i < expression.length(); i++) {
27+
char ch = expression.charAt(i);
2228
if (ch == ')') {
23-
if (st.peek() == '(') {
29+
if (stack.isEmpty() || stack.peek() == '(') {
2430
return true;
25-
} else {
26-
while (st.size() > 0 && st.peek() != '(') {
27-
st.pop();
28-
}
29-
st.pop();
31+
}
32+
while (!stack.isEmpty() && stack.peek() != '(') {
33+
stack.pop();
34+
}
35+
if (!stack.isEmpty()) {
36+
stack.pop();
3037
}
3138
} else {
32-
st.push(ch);
39+
stack.push(ch);
3340
}
34-
// System.out.println(st);
3541
}
3642
return false;
3743
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.thealgorithms.stacks;
2+
3+
import static org.junit.jupiter.api.Assertions.assertFalse;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
import static org.junit.jupiter.api.Assertions.assertTrue;
6+
7+
import org.junit.jupiter.api.Test;
8+
import org.junit.jupiter.params.ParameterizedTest;
9+
import org.junit.jupiter.params.provider.CsvSource;
10+
11+
class DuplicateBracketsTest {
12+
13+
@ParameterizedTest
14+
@CsvSource({"'((a + b) + (c + d))'", "'(a + b)'", "'a + b'", "'('", "''"})
15+
void testInputReturnsFalse(String input) {
16+
assertFalse(DuplicateBrackets.check(input));
17+
}
18+
19+
@ParameterizedTest
20+
@CsvSource({"'(a + b) + ((c + d))'", "'((a + b))'", "'((((a + b)))))'"})
21+
void testInputReturnsTrue(String input) {
22+
assertTrue(DuplicateBrackets.check(input));
23+
}
24+
25+
@Test
26+
void testInvalidInput() {
27+
assertThrows(IllegalArgumentException.class, () -> DuplicateBrackets.check(null));
28+
}
29+
}

0 commit comments

Comments
 (0)