-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathduplicate_brackets_check.java
46 lines (43 loc) · 1.28 KB
/
duplicate_brackets_check.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
46
// Print False for not having duplicate brackets
// True for having duplicate brackets
import java.io.*;
import java.util.*;
public class Program
{
public static void main(String[] args)
{
//input liya
Scanner scn=new Scanner(System.in);
String str=scn.nextLine();
//stack for character
Stack <Character> st=new Stack<>();
for(int i=0;i<str.length();i++)
{
char ch=str.charAt(i);
if(ch==')')
{
//agar character closing bracket hei to fir check whether the opnening bracket aa rha hei kya ya fir jab tak opening bracket nhi aaye tab tak pop karna hei
if(st.peek()=='(')
{
//agar peek par opening bracket hei to true
System.out.println("True");
return;
}
else
{
while(st.peek()!='(')
{
st.pop();
}
st.pop();
}
}
else
{
//closing bracket na hei character to push character in stack
st.push(ch);
}
}
System.out.println("False");
}
}