본문 바로가기

LeetCode/String

[Easy] 20. Valid Parentheses

#문제 설명

#문제 풀이

class Solution {
    public boolean isValid(String s) {
	Stack<Character> stack = new Stack<Character>();
	for (char c : s.toCharArray()) {
		if (c == '(')
			stack.push(')');
		else if (c == '{')
			stack.push('}');
		else if (c == '[')
			stack.push(']');
		else if (stack.isEmpty() || stack.pop() != c)
			return false;
	}
	return stack.isEmpty();
	}
}

위와 같이 Stack을 사용하면 쉽게 풀 수 있다.