문제 : https://programmers.co.kr/learn/courses/30/lessons/12909
코딩테스트 연습 - 올바른 괄호
괄호가 바르게 짝지어졌다는 것은 '(' 문자로 열렸으면 반드시 짝지어서 ')' 문자로 닫혀야 한다는 뜻입니다. 예를 들어 "()()" 또는 "(())()" 는 올바른 괄호입니다. ")()(" 또는 "(()(" 는 올바르지 않은
programmers.co.kr
문제유형 : 구현
풀이방식 : 단순구현 문제
leftCount => '(' 의 개수
rightCount => ')' 의 개수
소스코드
class Solution {
boolean solution(String s) {
boolean answer = true;
int sLength = s.length();
int leftCount = 0;
int rightCount = 0;
for(int i=0; i<sLength; i++){
if(s.charAt(i)=='('){
leftCount++;
}else{
rightCount++;
if(leftCount<rightCount){
answer = false;
break;
}
}
}
if(leftCount != rightCount) {
answer = false;
}
return answer;
}
}
출처: https://kistone.tistory.com/69?category=921501 [홧~팅~홧~팅]
'알고리즘 > 프로그래머스' 카테고리의 다른 글
[JAVA]프로그래머스_게임맵최단거리 (0) | 2021.08.05 |
---|---|
[JAVA]프로그래머스_기능개발 (0) | 2021.08.03 |
[JAVA]프로그래머스_짝지어 제거하기 (0) | 2021.07.26 |
[JAVA]프로그래머스_스킬트리 (0) | 2021.04.06 |
[JAVA]프로그래머스_프린터 (0) | 2021.04.06 |