240909

문제 : 카드 뭉치 난이도 : Lv.1

풀이

function solution(cards1, cards2, goal) {
    let index1 = 0;  // cards1의 현재 인덱스
    let index2 = 0;  // cards2의 현재 인덱스
    
    // goal의 단어를 순서대로 확인
    for (let word of goal) {
        // cards1에서 단어를 사용해야 할 경우
        if (index1 < cards1.length && cards1[index1] === word) {
            index1++;  // cards1의 다음 단어로 이동
        }
        // cards2에서 단어를 사용해야 할 경우
        else if (index2 < cards2.length && cards2[index2] === word) {
            index2++;  // cards2의 다음 단어로 이동
        }
        // 둘 다 아닐 경우 목표 단어 배열을 만들 수 없음
        else {
            return "No";
        }
    }
    
    // 모든 단어가 성공적으로 사용되었을 경우
    return "Yes";
}

Last updated