Skip to content

Commit d2e3f20

Browse files
committed
check balanced paranthesis
1 parent 936499b commit d2e3f20

1 file changed

Lines changed: 50 additions & 0 deletions

File tree

Stack/stackparanthesis.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import java.util.*;
2+
import java.util.Stack;
3+
public class stackparanthesis {
4+
5+
public static void main(String[] args)
6+
{
7+
8+
Stack<Character> stack=new Stack<Character>();
9+
Scanner s1=new Scanner(System.in);
10+
System.out.print("Enter the String containing brackets : ");
11+
String arr=s1.nextLine();
12+
if(arr.length()%2!=0)
13+
{
14+
System.out.println("String is Not Balanced");
15+
16+
}
17+
else
18+
{
19+
for(int i=0;i<arr.length();i++)
20+
{
21+
char ch=arr.charAt(i);
22+
if(ch=='{' || ch=='[' || ch=='(')
23+
{
24+
stack.push(ch);
25+
}
26+
else
27+
{
28+
if(!stack.isEmpty())
29+
{
30+
char c=stack.peek();
31+
if((ch=='}' && c=='{') || (ch==']' && c=='[') ||(ch==')' && c=='('))
32+
{
33+
stack.pop();
34+
}
35+
}
36+
}
37+
}
38+
if(stack.isEmpty())
39+
{
40+
System.out.println("String is Balanced");
41+
}
42+
else
43+
{
44+
System.out.println("String is Not Balanced");
45+
}
46+
47+
}
48+
}
49+
50+
}

0 commit comments

Comments
 (0)