#문제 설명
#문제 풀이
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을 사용하면 쉽게 풀 수 있다.
'LeetCode > String' 카테고리의 다른 글
[Easy] 125. Valid Palindrome (0) | 2022.11.17 |
---|---|
[Medium] 49. Group Anagrams (Amazon) (1) | 2022.09.20 |
[Medium] 1663. Smallest String With A Given Numeric Value (Microsoft) (0) | 2022.09.20 |
[Medium] 856. Score of Parentheses (0) | 2022.09.12 |
[Medium] 5. Longest Palindromic Substring (구글 코딩 테스트 기출 문제) (0) | 2022.09.11 |