[백준] 5052. 전화번호 목록

sort가 핵심!

import java.util.Arrays;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int T = sc.nextInt();

		for(int i=0; i<T; i++) {
			int N = sc.nextInt();
			String[] arr = new String[N];
			int cnt=0;

			for(int j=0; j<N; j++) {
				arr[j] = sc.next();
			}

			Arrays.sort(arr);

			for(int j=0; j<N-1; j++) {
				if(arr[j+1].startsWith(arr[j])) {
					cnt++;
					break;
				}
			}
			
			if(cnt==0)
				System.out.println("YES");
			else
				System.out.println("NO");
		}
	}
}

좋은 웹페이지 즐겨찾기