-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbalanced_brackets.java
More file actions
47 lines (44 loc) · 1.48 KB
/
Copy pathbalanced_brackets.java
File metadata and controls
47 lines (44 loc) · 1.48 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
47
static String[] braces(String[] values) {
String [] res = new String[values.length];
Stack<Character> s = new Stack<Character>();
for(int i=0;i<values.length;i++)
{
int flg=0;
String t =values[i];
for(int j=0;j<t.length();j++)
{
if((t.charAt(j)=='{')||(t.charAt(j)=='[')||(t.charAt(j)=='('))
{
s.push(t.charAt(j));
}
if((t.charAt(j)=='}')||(t.charAt(j)==']')||(t.charAt(j)==')'))
{
Character test = s.pop();
if((t.charAt(j)=='}'))
{
if(test=='{')
continue;
else
{res[i]="NO";flg=1;break;}
}
if((t.charAt(j)==']'))
{
if(test=='[')
continue;
else
{res[i]="NO";flg=1;break;}
}
if((t.charAt(j)==')'))
{
if(test=='(')
continue;
else
{res[i]="NO";flg=1;break;}
}
}
if(flg!=1)
res[i] = "YES";
}
}
return res;
}