-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDuplicatebracket.java
More file actions
42 lines (34 loc) · 1.12 KB
/
Duplicatebracket.java
File metadata and controls
42 lines (34 loc) · 1.12 KB
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
import java.util.Scanner;
import java.util.Stack;
public class Duplicatebracket {
public static boolean hasDuplicateBrackets(String str) {
Stack<Character> stack = new Stack<>();
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if (ch == ')') {
if (stack.peek() == '(') {
return true;
} else {
while (stack.peek() != '(') {
stack.pop();
}
stack.pop();
}
} else {
stack.push(ch);
}
}
return false;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the expression: ");
String input = sc.nextLine();
if (hasDuplicateBrackets(input)) {
System.out.println("Duplicate brackets are available.");
} else {
System.out.println("Duplicate brackets are not available.");
}
sc.close();
}
}