-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create duplicate_brackets_check.java
- Loading branch information
1 parent
e76828c
commit 6e7eea4
Showing
1 changed file
with
46 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |