We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 7a57dcf commit 65e4a63Copy full SHA for 65e4a63
1 file changed
2025-09-03/김대환/프로그래머스_짝지어_제거하기.java
@@ -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