Curriculum Design

Problem

Hyeonsu must plan a one-year course.
There are compulsory subjects in class. These compulsory courses must be completed, and the order is set.
If there are total subjects A, B, C, D, E, F, G, and the compulsory subjects are given as CBA, the compulsory subjects are C, B, and A subjects, and you must plan the lesson in this order.
The order here is that subject B must be taken after completing course C, and subject A must be taken after completing both C and B.
If Hyeonsu makes a lesson plan with C, B, D, A, G, E, it is a good design, but if Hyeonsu writes it in the order of C, G, E, A, D, B, it is a badly designed lesson plan.
The lesson plan is interpreted as starting the next lesson when the preceding lessons are completed in that order.
It is assumed that each subject in the syllabus is unconditionally completed.
If the required subject order is given, write a program that prints “YES” if the N class design created by Hyun-soo is good, and “NO” if it is wrong.

input

On the first line, the order of the required subjects is given on one line. All subjects are in uppercase English.

CBA

CBDAGE

output

From the second line, a lesson design woven by Hyunsu is given. (The length of the lesson design is 30 or less)

YES

Solution

handle only string

function solution(need, plan) {
	let answer = 'YES';

	for (let x of plan) {
		if (need.includes(x)) {
			if (need[0] === x) need = need.substring(1);
			else return 'NO';
		}
	}

	if (need.length) return 'NO';

	return answer;
}

using queue

function solution(need, plan) {
	let answer = 'YES';

	let queue = need.split('');
	for (let x of plan) {
		if (queue.includes(x)) {
			if (x !== queue.shift()) return 'NO';
		}
	}

	if (queue.length) return 'NO';

	return answer;
}