Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

21.03.22 - [BOJ] 4889. 안정적인 문자열 #38

Closed
suhyunsim opened this issue Mar 15, 2021 · 0 comments
Closed

21.03.22 - [BOJ] 4889. 안정적인 문자열 #38

suhyunsim opened this issue Mar 15, 2021 · 0 comments
Assignees
Labels
성공 맞은 문제 스택 stack 실버 BOJ - 실버

Comments

@suhyunsim
Copy link
Owner

suhyunsim commented Mar 15, 2021

문제

핵심 아이디어

  • 단순 스택 문제

어려운 점, 실수

풀이

package main.java.com.poogle.BOJ.Q4889;

import java.util.Scanner;
import java.util.Stack;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int i = 1;
        while (true) {
            String s = sc.next();
            if (s.startsWith("-")) break;
            System.out.println(i++ + ". " + check(s));
        }
    }

    private static int check(String input) {
        int cnt = 0;
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < input.length(); i++) {
            char c = input.charAt(i);
            if (c == '{') {
                stack.push(c);
            } else if (c == '}') {
                if (stack.isEmpty()) {
                    stack.push(c);
                } else if (stack.peek() == '{') {
                    stack.pop();
                } else {
                    stack.push(c);
                }
            }
        }

        while (!stack.isEmpty()) {
            char c = stack.pop();
            if (c == '{') {
                if (stack.peek() == '{') {
                    cnt++;
                } else {
                    cnt += 2;
                }
            } else {
                cnt++;
            }
            stack.pop();
        }
        return cnt;
    }

}
@suhyunsim suhyunsim added 실버 BOJ - 실버 스택 stack labels Mar 15, 2021
@suhyunsim suhyunsim added this to the 3월 3주 차 milestone Mar 15, 2021
@suhyunsim suhyunsim self-assigned this Mar 15, 2021
@suhyunsim suhyunsim changed the title 21.03.16 - [BOJ] 4889. 안정적인 문자열 21.03.22 - [BOJ] 4889. 안정적인 문자열 Mar 22, 2021
@suhyunsim suhyunsim modified the milestones: 3월 3주 차, 3월 4주 차 Mar 22, 2021
@suhyunsim suhyunsim added the 성공 맞은 문제 label Jan 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
성공 맞은 문제 스택 stack 실버 BOJ - 실버
Projects
None yet
Development

No branches or pull requests

1 participant