Skip to content

Commit 65e4a63

Browse files
committed
20250903 프로그래머스 짝지어 제어하기 문제풀이
1 parent 7a57dcf commit 65e4a63

1 file changed

Lines changed: 23 additions & 0 deletions

File tree

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// 스택으로 풀이
2+
// 왼쪽부터 문자를 하나씩 읽는다.
3+
// 스택이 비어있지 않고 top == 현재 문자이면, 두 문자가 짝이 되어 제거(pop) 된다.
4+
// 그렇지 않으면 현재 문자를 push한다.
5+
// 전부 처리한 뒤 스택이 비어 있으면 1, 아니면 0을 반환한다.
6+
7+
class Solution {
8+
public int solution(String s) {
9+
char[] stack = new char[s.length()];
10+
int top = -1; // 비어있음
11+
12+
for (int i = 0; i < s.length(); i++) {
13+
char c = s.charAt(i);
14+
15+
if (top >= 0 && stack[top] == c) {
16+
top--; // pop: 인접 동일 문자 쌍 제거, 포인터만 이동
17+
} else {
18+
stack[++top] = c; // push
19+
}
20+
}
21+
return (top == -1) ? 1 : 0;
22+
}
23+
}

0 commit comments

Comments
 (0)