본문 바로가기
코딩테스트 준비/프로그래머스

프로그래머스 C# - 올바른 괄호

by 코딩하는 돼징 2023. 9. 17.
반응형

봤던 문제라 금방 풀었다!


using System;
using System.Collections.Generic;
public class Solution {
    public bool solution(string s) {
        bool answer = true;

        int right = 0;
        int left = 0;

        foreach(char c in s)
        {
            if(c=='(') left++;
            else
                right++;
            if(left < right)
            {
                answer = false;
                return answer;
            }
        }

        if(left != right)
        {
            answer = false;
            return answer;
        }

        return answer;
    }
}

백준에 비슷한 문제가 있었다.

백준 C# - 9012

 

백준 C# - 9012

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace baek2 { class Program { static void Main(string[] args) { int num = int.Parse(Console.ReadLine()); for (int i = 0; i < num; i++) { string answer = "YES"; string

code-piggy.tistory.com

 

반응형

댓글