Skip to content

Commit 3c208db

Browse files
Fix: fully Checkstyle-compliant and optimized BalancedBrackets
1 parent ff97d47 commit 3c208db

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

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

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,22 @@
44
import java.util.Deque;
55

66
/**
7-
* Optimized and robust Balanced Parenthesis (Valid Brackets) checker.
7+
* BalancedBrackets.java
8+
* Optimized valid parenthesis checker
89
*
910
* Supports: (), [], {}, <>
11+
*
1012
* Rules:
1113
* - Returns true if brackets are properly nested and matched.
1214
* - Returns false for any non-bracket character.
1315
* - Empty string is balanced.
1416
* - Null input throws IllegalArgumentException.
15-
* @author Basundhara
16-
* @author <a href="https://github.com/coder-Basundhara">GitHub</a>
17+
*
1718
* Time complexity: O(n)
1819
* Space complexity: O(n) in worst case (stack contains all opening brackets).
20+
*
21+
* @author Basundhara
22+
* @author <a href="https://github.com/coder-Basundhara">GitHub</a>
1923
*/
2024
public final class BalancedBrackets {
2125

@@ -25,6 +29,10 @@ private BalancedBrackets() {
2529

2630
/**
2731
* Returns true if {@code opening} and {@code closing} are matching bracket pair.
32+
*
33+
* @param opening opening bracket
34+
* @param closing closing bracket
35+
* @return true if matched
2836
*/
2937
public static boolean isPaired(char opening, char closing) {
3038
return (opening == '(' && closing == ')')
@@ -36,6 +44,8 @@ public static boolean isPaired(char opening, char closing) {
3644
/**
3745
* Checks if the input string has balanced brackets.
3846
*
47+
* @param input input string
48+
* @return true if balanced
3949
* @throws IllegalArgumentException when input is null
4050
*/
4151
public static boolean isBalanced(String input) {
@@ -47,7 +57,7 @@ public static boolean isBalanced(String input) {
4757
return true;
4858
}
4959

50-
// Odd-length strings cannot be fully balanced
60+
// Odd-length strings cannot be balanced
5161
if ((input.length() & 1) == 1) {
5262
return false;
5363
}
@@ -62,7 +72,6 @@ public static boolean isBalanced(String input) {
6272
case '<':
6373
stack.push(c);
6474
break;
65-
6675
case ')':
6776
case ']':
6877
case '}':
@@ -71,7 +80,6 @@ public static boolean isBalanced(String input) {
7180
return false;
7281
}
7382
break;
74-
7583
default:
7684
// Any non-bracket character makes string invalid
7785
return false;
@@ -83,7 +91,7 @@ public static boolean isBalanced(String input) {
8391

8492
/**
8593
* Optional main method for quick manual testing
86-
94+
*/
8795
public static void main(String[] args) {
8896
String[] tests = {
8997
"()",
@@ -98,11 +106,10 @@ public static void main(String[] args) {
98106

99107
for (String t : tests) {
100108
try {
101-
System.out.println(t + " -> " + isBalanced(t));
102-
} catch (IllegalArgumentException e) {
103-
System.out.println(t + " -> Error: " + e.getMessage());
109+
System.out.printf("%s -> %b%n", t, isBalanced(t));
110+
} catch (Exception e) {
111+
System.out.printf("%s -> Exception: %s%n", t, e.getMessage());
104112
}
105113
}
106114
}
107-
**/
108115
}

0 commit comments

Comments
 (0)