-
Notifications
You must be signed in to change notification settings - Fork 0
/
sudeep.java
45 lines (35 loc) · 1.36 KB
/
sudeep.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import java.util.Stack;
public class BalancedBrackets {
public static boolean isBalancedBrackets(String inputString) {
Stack<Character> stack = new Stack<>();
char[] chars = inputString.toCharArray();
for (char c : chars) {
if (c == '(' || c == '[' || c == '{') {
stack.push(c);
} else if (c == ')' || c == ']' || c == '}') {
if (stack.isEmpty()) {
return false;
}
char top = stack.pop();
if ((c == ')' && top != '(') || (c == ']' && top != '[') || (c == '}' && top != '{')) {
return false;
}
}
}
return stack.isEmpty();
}
public static void main(String[] args) {
String input1 = "([[{ }]])";
String input2 = "([[{ }]]))";
if (isBalancedBrackets(input1)) {
System.out.println("The entered String has Balanced Brackets");
} else {
System.out.println("The entered String does not contain Balanced Brackets");
}
if (isBalancedBrackets(input2)) {
System.out.println("The entered String has Balanced Brackets");
} else {
System.out.println("The entered String does not contain Balanced Brackets");
}
}
}