-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBalanceParen.java
More file actions
42 lines (36 loc) · 801 Bytes
/
BalanceParen.java
File metadata and controls
42 lines (36 loc) · 801 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
package edu.buffalo.liveramp;
import java.util.Stack;
public class BalanceParen {
public static void main(String[] args) {
BalanceParen bp = new BalanceParen();
bp.balanced("(])");
}
private void balanced(String string) {
char[] c = string.toCharArray();
if(c.length==1)
System.out.println("no");
Stack<Character> s = new Stack<Character>();
for(int i = 0;i<c.length;i++)
{
if(c[i]=='(' || c[i]=='[' || c[i]=='{')
{
s.push(c[i]);
}
if(c[i]==')' || c[i]==']' || c[i]=='}')
{
if(!s.isEmpty() && ((s.peek()=='(' && c[i]==')') || (s.peek()=='{' && c[i]=='}') || (s.peek()=='[' && c[i]==']')))
{
s.pop();
}
else
{
//no
}
}
}
if(s.isEmpty())
System.out.println("yes");
else
System.out.println("no");
}
}