Skip to content

Commit

Permalink
[level 2] Title: 올바른 괄호, Time: 18.50 ms, Memory: 53 MB -BaekjoonHub
Browse files Browse the repository at this point in the history
  • Loading branch information
shinjaewon99 committed Sep 22, 2023
1 parent fc299ce commit 21991ed
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
68 changes: 68 additions & 0 deletions 프로그래머스/lv2/12909. 올바른 괄호/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# [level 2] 올바른 괄호 - 12909

[문제 링크](https://school.programmers.co.kr/learn/courses/30/lessons/12909)

### 성능 요약

메모리: 53 MB, 시간: 18.50 ms

### 구분

코딩테스트 연습 > 스택/큐

### 채점결과

Empty

### 문제 설명

<p>괄호가 바르게 짝지어졌다는 것은 '(' 문자로 열렸으면 반드시 짝지어서 ')' 문자로 닫혀야 한다는 뜻입니다. 예를 들어</p>

<ul>
<li>"()()" 또는 "(())()" 는 올바른 괄호입니다.</li>
<li>")()(" 또는 "(()(" 는 올바르지 않은 괄호입니다.</li>
</ul>

<p>'(' 또는 ')' 로만 이루어진 문자열 s가 주어졌을 때, 문자열 s가 올바른 괄호이면 true를 return 하고, 올바르지 않은 괄호이면 false를 return 하는 solution 함수를 완성해 주세요.</p>

<h5>제한사항</h5>

<ul>
<li>문자열 s의 길이 : 100,000 이하의 자연수</li>
<li>문자열 s는 '(' 또는 ')' 로만 이루어져 있습니다.</li>
</ul>

<hr>

<h5>입출력 예</h5>
<table class="table">
<thead><tr>
<th>s</th>
<th>answer</th>
</tr>
</thead>
<tbody><tr>
<td>"()()"</td>
<td>true</td>
</tr>
<tr>
<td>"(())()"</td>
<td>true</td>
</tr>
<tr>
<td>")()("</td>
<td>false</td>
</tr>
<tr>
<td>"(()("</td>
<td>false</td>
</tr>
</tbody>
</table>
<h5>입출력 예 설명</h5>

<p>입출력 예 #1,2,3,4<br>
문제의 예시와 같습니다.</p>


> 출처: 프로그래머스 코딩 테스트 연습, https://programmers.co.kr/learn/challenges
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import java.util.Stack;

class Solution {
boolean solution(String s) {
boolean answer = true;

Stack<Character> store = new Stack<>();

for(int i = 0; i < s.length(); i++){
char input = s.charAt(i);

// 1. 여는 괄호 일경우
if(input == '('){
store.push(input);
}else{
// 2. 스택이 비어있으면 닫는 괄호가 있을수 있음
if(store.isEmpty()) return false;
// 3. 스택이 비어있지 않다는것은 여는 괄호가 있음
else store.pop();
}
}

// 4. 여는괄호가 남아있을경우
if(!store.isEmpty()) return false;

return answer;
}
}

0 comments on commit 21991ed

Please sign in to comment.