class Solution {
public boolean isValid(String s) {
if (s.length() % 2 != 0) {
return false;
}
Map<Character, Character> set = new HashMap<Character, Character>();
set.put(']', '[');
set.put(')', '(');
set.put('}', '{');
Stack<Character> check = new Stack<Character>();
for (int i = 0; i < s.length(); i++) {
if (!set.containsKey(s.charAt(i))) {
check.push(s.charAt(i));
} else {
if (check.isEmpty()) {
return false;
}
if (check.pop() != set.get(s.charAt(i))) {
return false;
}
}
}
return (check.isEmpty());
}
}
Runtime: 2 ms, faster than 23.32% of Java online submissions for Valid Parentheses.
Memory Usage: 37.4 MB, less than 20.24% of Java online submissions for Valid Parentheses.
느린데 고치기 너무 귀찮다..^^