✔️ 내 답-> 정답
맨 처음에 필수과목 must, 시간표 sub를 둘다 queue로 했더니 시간 초과가 나와 must만 queue로 했더니 정답으로 나왔다.
import java.util.*;
public class Main{
public static String solution(String must, String sub){
int pointer =0;
Queue<Character> dq = new LinkedList<>();
for(char x: must.toCharArray()){
dq.add(x);
}
for(int j=0;j<must.length();j++){
char value= dq.peek();
while(pointer<sub.length()){
if(sub.charAt(pointer)==value) {
dq.poll();
pointer++;
break;
}
else pointer++;
}
}
if(dq.size()==0) return "YES";
else return "NO";
}
public static void main(String[] args){
Scanner kb = new Scanner(System.in);
String must = kb.next();
String sub = kb.next();
System.out.println(solution(must,sub));
}
}
✔️ 강의 듣고 풀기
import java.util.*;
class Main{
public static String solution(String a, String b){
String answer ="YES";
Queue<Character> Q = new LinkedList<>();
for(char x : a.toCharArray()) Q.offer(x);
for(char x : b.toCharArray()) {
if(Q.contains(x)) {
if(x!=Q.poll()) return "NO";
}
}
if(!Q.isEmpty()) return "NO";
return answer;
}
public static void main(String[] args){
Scanner kb = new Scanner(System.in);
String a =kb.next();
String b = kb.next();
System.out.println(solution(a,b));
}
}