-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathBalancedParanthesis.java
More file actions
46 lines (43 loc) · 1.05 KB
/
BalancedParanthesis.java
File metadata and controls
46 lines (43 loc) · 1.05 KB
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
class Solution
{
//Function to check if brackets are balanced or not.
public static boolean opening(char ch)
{
switch(ch)
{
case '{':
return true;
case '(':
return true;
case '[':
return true;
}
return false;
}
public static boolean Matches(char a, char b)
{
if(a=='{' && b=='}') return true;
if(a=='(' && b==')') return true;
if(a=='[' && b==']') return true;
return false;
}
static boolean ispar(String x)
{
// add your code here
Stack<Character> stack = new Stack<Character>();
for(int i=0;i<x.length();i++)
{
if(opening(x.charAt(i)))
{
stack.push(x.charAt(i));
}
else
{
if(stack.isEmpty()) return false;
if(!Matches(stack.peek(),x.charAt(i))) return false;
stack.pop();
}
}
return stack.isEmpty();
}
}