
문제에서 주어진 문자열이 (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);
}
}