import java.util.*;
class Solution {
public boolean isValid(String s) {
Stack<Character> _stack = new Stack<>();
char parentheses[] = s.toCharArray();
for (int i = 0; i < parentheses.length; i++) {
if (_stack.empty() && (parentheses[i] == ')' || parentheses[i] == ']' || parentheses[i] == '}')) {
return false;
}
if (parentheses[i] == '(' || parentheses[i] == '{' || parentheses[i] == '[') {
_stack.push(parentheses[i]);
//System.out.println(_stack);
// System.out.println("Is Empty: " + _stack.empty());
} else if (parentheses[i] == ')') {
if (_stack.peek() == '(') {
//System.out.println(_stack.peek());
_stack.pop();
} else {
return false;
}
} else if (parentheses[i] == '}') {
if (_stack.peek() == '{') {
//System.out.println(_stack.peek());
_stack.pop();
} else {
return false;
}
} else if (parentheses[i] == ']') {
if (_stack.peek() == '[') {
//System.out.println(_stack.peek());
_stack.pop();
} else {
return false;
}
}
}
if (_stack.empty()) {
return true;
}
return false;
}
public static void main(String[] args) {
Solution sol = new Solution();
String input1 = "()";
boolean output1 = sol.isValid(input1);
System.out.println(output1);
String input2 = "()[]{}";
boolean output2 = sol.isValid(input2);
System.out.println(output2);
String input3 = "(]";
boolean output3 = sol.isValid(input3);
System.out.println(output3);
String input4 = "([)]";
boolean output4 = sol.isValid(input4);
System.out.println(output4);
String input5 = "{[]}";
boolean output5 = sol.isValid(input5);
System.out.println(output5);
}
}
후....
https://leetcode.com/problems/valid-parentheses/
쉬운거니
나처럼 초보들은 풀기 좋다....
프갤 상위
? 3주 차 다... 희망은 고맙다....
문제 재밌겠네
리트코드.... 좋다.... 후.....
어느기업코테준비하냐 - dc App
중소기업 아무곳이나다.... 나 받아주는곳..... 비전공 노 국비 공부 3주차다...
중소에서 코테를보나.. 쨋든화팅이다 - dc App