[백준 코딩테스트] 1013번 Contact

gyeol·2025년 8월 22일

코딩테스트 공부

목록 보기
48/53
post-thumbnail

풀이

문제에서 주어진 문자열이 (100+1+|01)+ 의 형태와 일치하는지 검사해야 한다.
보통 자바에서는 정규표현식과 매칭되는지 알아보기 위해 String.matches() 메서드를 사용한다.

if (input.matches("정규식")) {
    // 전체 문자열이 정규식과 정확히 일치하면 true
}
  • 문자열 전체가 정규표현식과 일치해야 true 반환
  • 부분 일치가 아닌 완전 매칭 !

이 표현식을 알고있다면 손쉽게 풀 수 있는 문제였다.

내 코드

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
       BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb = new StringBuilder();

        int n = Integer.parseInt(br.readLine());
        String pattern = "(100+1+|01)+";

        for(int i=0; i<n; i++){
            String input = br.readLine();
            if(input.matches(pattern)){
                sb.append("YES\n");
            }
            else{
                sb.append("NO\n");
            }
        }

        System.out.println(sb);
    }
}
profile
공부 기록 공간 '◡'

0개의 댓글