230914 올바른 괄호

Jongleee·2023년 9월 14일
0

TIL

목록 보기
364/683
boolean solution(String s) {
	return isCorrect(s);
}

private boolean isCorrect(String s) {
	Stack<Character> stack = new Stack<>();
	for (char c : s.toCharArray()) {
		if (c == '(') {
			stack.push(c);
		} else if (!stack.isEmpty() && stack.peek() == '(') {
			stack.pop();
		} else {
			stack.push(c);
		}
	}
	return stack.isEmpty();
}

출처:https://school.programmers.co.kr/learn/courses/30/lessons/12909

0개의 댓글