-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathParenthisischecker.java
More file actions
46 lines (40 loc) · 882 Bytes
/
Parenthisischecker.java
File metadata and controls
46 lines (40 loc) · 882 Bytes
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
import java.util.*;
public class Parenthisischecker {
static void checkparent(String str)
{
Stack<Character>s=new Stack<>();
for(int i1=0;i1<str.length();i1++)
{
char current=str.charAt(i1);
if(str.charAt(i1)=='['||str.charAt(i1)=='('||str.charAt(i1)=='{')
{
s.push(current);
}
else if(str.charAt(i1)==']'||str.charAt(i1)==')'||str.charAt(i1)=='}')
{
if(s.isEmpty())
{
System.out.print("Not Balanced");
}
char last=s.peek();
if(current == '}' && last == '{' || current == ')' && last == '(' || current == ']' && last == '[')
{
s.pop();
}
}
}
if(s.isEmpty())
{
System.out.print("Balanced ");
}
else
{
System.out.print("Not Balanced");
}
}
public static void main(String [] args)
{
String str="{}[][";
checkparent(str);
}
}