-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathED195.java
45 lines (37 loc) · 1.07 KB
/
ED195.java
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
public class ED195{
public static void reverse(MyStack<Character> s, int n){
char list[] = new char[n];
for(int i=0; i<n; i++){
char v = s.pop();
list[i] = v;
}
for(int j=0; j<n;j++){
char x = list[j];
s.push(x);
}
}
public static boolean balanced(String s){
boolean balanced=true;
MyStack<Character> aux = new LinkedListStack<>();
for(int i=0; i<s.length(); i++){
char x= s.charAt(i);
aux.push(x);
}
MyStack<Character> aux2 = new LinkedListStack<>();
for (int j=0; j<(aux.size()/2); j++){
aux2.push(aux.pop());
}
int size = aux2.size();
reverse(aux2,size);
int n = aux.size();
while(n>0){
if((aux.top()=='(' && aux2.top()!=')')||(aux.top()=='[' && aux2.top()!=']')){balanced=false;}
n--;
}
return balanced;
}
public static void main(String[] args) {
String s = "]()]";
System.out.println(balanced(s));
}
}