정렬, 해시, 트라이 여러 풀이가 있는데 트라이 공부를 위해 트라이 자료구조를 사용하여 풀이
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer st;
int t=Integer.parseInt(br.readLine());
while(t-->0){
int n=Integer.parseInt(br.readLine());
Trie trie=new Trie();
boolean flag=true;
for(int i=0;i<n;i++){
String s=br.readLine();
if(!trie.insert(s)){
flag=false;
}
}
System.out.println(flag?"YES":"NO");
}
}
static class Node{
HashMap<Character,Node> childs=new HashMap<>();
boolean stringEnd=false;
}
static class Trie{
Node root=new Node();
boolean insert(String num){
Node now=root;
for(int i=0;i<num.length();i++){
if(!now.childs.containsKey(num.charAt(i))){
now.childs.put(num.charAt(i),new Node());
}
now=now.childs.get(num.charAt(i));
if(now.stringEnd){
return false;
}
}
if(!now.childs.isEmpty())return false;
now.stringEnd=true;
return true;
}
}
}
#트라이