1. 문제
출처 : 프로그래머스
is_pair함수는 문자열 s를 매개변수로 입력받습니다.
s에 괄호가 알맞게 짝지어져 있으면 True를 아니면 False를 리턴하는 함수를 완성하세요.
예를들어 s가 (hello)()면 True이고, )(이면 False입니다.
s가 빈 문자열("")인 경우는 없습니다.
2. 코드
package Test; public class Pair { public static void main(String[] args) { String s = "((Hello))"; Pair pair = new Pair(); System.out.println(pair.check(s)); } private boolean check(String s) { boolean result = true; int count = 0; for (int inx = 0; inx < s.length(); inx++) { char c = s.charAt(inx); if (c == '(') count++; else if (c == ')') count--; if (count < 0) { result = false; break; } } return result; } }