Skip to content

Commit

Permalink
Create duplicate_brackets_check.java
Browse files Browse the repository at this point in the history
  • Loading branch information
suhanigupta23 authored Feb 17, 2024
1 parent e76828c commit 6e7eea4
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions duplicate_brackets_check.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,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");
}
}

0 comments on commit 6e7eea4

Please sign in to comment.