
import java.util.Stack;
class Solution {
boolean solution(String s) {
Stack<Character> stack = new Stack<>();
for (int i = 0; i < s.length(); i++) {
char at = s.charAt(i);
if (at == '(') {
stack.push(at);
} else if (at == ')') {
if (!stack.isEmpty() && stack.peek() == '(') { //스택 가장 위에 값이 '('이면
stack.pop(); // '('에 대응되는 ')'가 있으면 pop //괄호가 맞으면 스택에서 비움
} else {
return false; // '('에 대응되는 ')'가 없으면 유효하지 않은 괄호 문자열
}
}
}
return stack.isEmpty(); // 괄호가 다 맞아서 스택이 비어있으면 true
}
}
import java.util.Stack;
class Solution {
boolean solution(String s) {
Stack<Character> stack = new Stack<>();
boolean answer = false;
int count = 0;
int count2 = 0;
//스택에 데이터 push
for (int i = 0; i < s.length(); i++) {
stack.push(s.charAt(i));
}
//맨 위에 값이 (면 무조건 false
if(stack.peek() == '(')
return false;
while (!stack.isEmpty()) {
char pop = stack.pop();
if (pop == ')') {
count++;
} else if (pop == '(') {
count2++;
if (count == count2) {
answer = true;
}
}
}
return answer;
}
}

https://school.programmers.co.kr/learn/courses/30/lessons/12909
import java.util.Stack;
public class StackExample {
public static void main(String[] args) {
// 정수형 데이터를 담을 수 있는 스택 생성
Stack<Integer> stack = new Stack<>();
// 스택에 데이터 추가 (push)
stack.push(1);
// 스택에서 데이터 꺼내기 (pop)
int poppedValue = stack.pop();
// 스택의 맨 위 데이터 확인 (peek)
int topValue = stack.peek();
// 스택이 비어 있는지 확인
boolean isEmpty = stack.isEmpty();
// 스택의 크기 확인
int size = stack.size();
}
}