Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 수학
- 스택
- 유니온 파인드
- java
- 후니의 쉽게 쓴 시스코 네트워킹
- BFS
- Kotlin
- CS
- 투 포인터
- 백트래킹
- 백준
- 그리디
- JUnit 5
- 동적계획법
- 문자열
- mst
- 플로이드-와샬
- swea
- 세그먼트 트리
- 위상정렬
- 이분탐색
- Effective Java
- 에라토스테네스의 체
- 완전탐색
- 프로그래머스
- 시뮬레이션
- Network
- 구현
- dfs
- 알고리즘
Archives
반갑습니다!
[프로그래머스] 괄호 변환 본문
풀이
문제가 길어서 어렵다고 겁먹을 수 있지만 명시된 규칙대로 구현하면 의외로 쉽게 해결할 수 있다. '균형잡힌 괄호 문자열'의 여부는 stack
을 이용하여 확인하였다.
자세한 풀이는 주석으로 남겨두었다.
코드
C++
#include <string>
#include <vector>
#include <stack>
using namespace std;
// 올바른 괄호 문자열인지 체크해주는 함수
bool isCorrect(string str) {
stack<char> s;
for (char c : str) {
if (c == '(') s.push(c);
else {
if (s.empty()) return false;
s.pop();
}
}
if (!s.empty()) return false;
return true;
}
// 문자열 u, v로 분리해주는 함수
pair<string, string> cutUV(string str) {
// 괄호의 개수를 체크하는 변수 '(' 이면 ++, ')' 이면 --
int cnt = 0;
int idx = 0;
for (char c : str) {
idx++;
if (c == '(') cnt++;
else cnt--;
// 괄호의 개수가 제일 처음 같아지는 경우 u, v를 분리한다
if (cnt == 0) break;
}
return { str.substr(0, idx), str.substr(idx, str.length() - idx) };
}
// 규칙 함수
string solution(string p) {
string answer = p;
// 올바른 괄호 문자열인 경우
if (isCorrect(answer)) return answer;
pair<string, string> s = cutUV(answer);
string u = s.first;
string v = s.second;
// u가 올바른 괄호 문자열인 경우
if (isCorrect(u)) answer = u + solution(v);
else {
answer = "(" + solution(v) + ")";
// u의 앞, 뒤 괄호를 제거
u = u.substr(1, u.length() - 2);
// u의 괄호를 뒤집는 부분
for (int i = 0; i < u.length(); i++) {
char c = u[i];
if (c == '(') u[i] = ')';
else u[i] = '(';
}
answer += u;
}
return answer;
}
Python3
def solution(p):
if is_correct(p):
return p
u, v = divide_string(p)
if is_correct(u):
return u + solution(v)
else:
temp = '(' + solution(v) + ')'
u = list(u[1:-1])
for i in range(len(u)):
if u[i] == '(':
u[i] = ')'
else:
u[i] = '('
return temp + "".join(u)
def is_correct(p):
stack = []
for i in p:
if i == '(':
stack.append(i)
else:
if len(stack) == 0:
return False
stack.pop()
if len(stack) == 0:
return True
return False
def divide_string(p):
idx = 0
count = 0
for i in range(len(p)):
idx += 1
if p[i] == '(':
count += 1
else:
count -= 1
if count == 0:
break
u = p[:idx]
v = p[idx:]
return (u, v)
'알고리즘 문제 풀이' 카테고리의 다른 글
[프로그래머스] 정수 내림차순으로 배치하기 (0) | 2020.04.04 |
---|---|
[백준] 1926 그림 (0) | 2020.04.04 |
[프로그래머스] 짝지어 제거하기 (0) | 2020.04.03 |
[프로그래머스] JadenCase 문자열 만들기 (0) | 2020.04.03 |
[프로그래머스] 124 나라의 숫자 (0) | 2020.04.03 |