HelloMinchan
처음처럼
HelloMinchan
LinkedIn
전체 방문자
오늘
어제
  • 분류 전체보기 (306)
    • Backend (4)
      • NestJS (1)
      • Express (1)
      • Spring (2)
    • Infrastructure (1)
      • AWS (1)
    • Frontend (1)
      • Next.js (1)
    • Language & Runtime (4)
      • Java (2)
      • Node.js (2)
    • Computer Science (8)
      • Computer Networks (3)
      • Operating Systems (4)
      • OOP (1)
    • 독서 (4)
      • 데이터 중심 애플리케이션 설계 (3)
      • 객체지향의 사실과 오해 (1)
    • 회고 (4)
      • Project (2)
      • Career (2)
    • Deprecated (280)

채널

  • GitHub
  • LinkedIn

최근 글

태그

  • 프로그래머스C++
  • 백준
  • 백준C++
  • 데이터베이스
  • 백준Python
  • Algospot
  • front-end
  • 개발자
  • programmers
  • 코딩
  • 백준Go
  • 백엔드
  • back-end
  • 프로그래머스
  • 프로그래머스Python
  • 알고스팟Python
  • Baekjoon Online Judge
  • 알고스팟
  • Database
  • 프로그래밍

최근 댓글

인기 글

hELLO
HelloMinchan

처음처럼

[ALGOSPOT] 알고스팟 BRACKETS2 Mismatched Brackets(Python)
Deprecated

[ALGOSPOT] 알고스팟 BRACKETS2 Mismatched Brackets(Python)

2020. 5. 30. 15:28

www.algospot.com

[ALGOSPOT] 알고스팟 BRACKETS2 Mismatched Brackets

(Python)

(글쓴날 : 2020.05.30)

 


* ALGOSPOT, 알고스팟 BRACKETS2 문제 Python 언어 풀이입니다.

* 소스 코드의 저작권은 글쓴이에게 있습니다.


 

 

알고스팟 BRACKETS2 Mismatched Brackets


1) 문제

문제 링크 : https://algospot.com/judge/problem/read/BRACKETS2

 

algospot.com :: BRACKETS2

Mismatched Brackets 문제 정보 문제 Best White is a mathematics graduate student at T1 University. Recently, he finished writing a paper and he decided to polish it. As he started to read it from the beginning, he realized that some of the formulas ha

algospot.com


2) 풀이 과정

* 시간 복잡도 : O(n)

 

괄호들로 이루어진 문자열이 주어질 때, 괄호들의 짝이 맞는지 검사하는 문제입니다.

 

저의 경우, 스택을 적용하였고, Python을 사용했습니다.

여는 괄호일 경우 스택에 push 하고, 닫는 괄호일 경우 스택에서 pop 한 값을 비교하여 짝이 맞는지 검사하는 식으로 문제를 해결했습니다.


3) 코드

 

* Python 코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import sys
input = sys.stdin.readline
 
C = int(input())
 
for _ in range(C):
    brackets = list(input().rstrip())
    stack = []
    isMatched = True
 
    for bracket in brackets:
        if not len(stack):
            stack.append(bracket)
            continue
        if bracket == "(" or bracket == "{" or bracket == "[":
            stack.append(bracket)
            continue
        if bracket == ")" or bracket == "}" or bracket == "]":
            if bracket == ")":
                if stack.pop() != "(":
                    isMatched = False
                    break
            elif bracket == "}":
                if stack.pop() != "{":
                    isMatched = False
                    break
            else:
                if stack.pop() != "[":
                    isMatched = False
                    break
    
    if len(stack):
        isMatched = False
    
    print("YES" if isMatched else "NO")

 

저작자표시 비영리 변경금지

'Deprecated' 카테고리의 다른 글

[ALGOSPOT] 알고스팟 TRAVERSAL 트리 순회 순서 변경(Python)  (0) 2020.06.01
[ALGOSPOT] 알고스팟 ITES 외계 신호 분석(C++, Python)  (0) 2020.05.30
[Probability and Statistics] 순열과 조합  (0) 2020.05.29
[ALGOSPOT] 알고스팟 JOSEPHUS 조세푸스 문제(Python)  (0) 2020.05.29
[ALGOSPOT] 알고스팟 JUMPGAME 외발 뛰기(Python)  (0) 2020.05.26
    'Deprecated' 카테고리의 다른 글
    • [ALGOSPOT] 알고스팟 TRAVERSAL 트리 순회 순서 변경(Python)
    • [ALGOSPOT] 알고스팟 ITES 외계 신호 분석(C++, Python)
    • [Probability and Statistics] 순열과 조합
    • [ALGOSPOT] 알고스팟 JOSEPHUS 조세푸스 문제(Python)
    HelloMinchan
    HelloMinchan
    Though you should not fear failure, You should do your very best to avoid it.

    티스토리툴바