Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 1

// you can also use imports, for example:

import java.util.*;

// you can write to stdout for debugging purposes, e.g.


// System.out.println("this is a debug message");

class Solution {
public int solution(String S) {
int l = S.length();
ArrayList<String> list = new ArrayList();
char c;
String str;

for(int i = 0; i < l; i++) {


c = S.charAt(i);
if(c == '{' || c == '(' || c == '[') {
list.add(c + "");
} else {
if(list.isEmpty()) {
return 0;
}
str = list.get(list.size() - 1);
if(c == '}' && str.equals("{")) {
list.remove(list.size() - 1);
} else if(c == ']' && str.equals("[")){
list.remove(list.size() - 1);
} else if(c == ')' && str.equals("(")){
list.remove(list.size() - 1);
} else {
return 0;
}
}
}
return list.isEmpty() ? 1 : 0;
}
}

You might also like