본문 바로가기

자료구조+알고리즘/Programmers

[프로그래머스_lv1] 카드 뭉치 (javascript)

https://school.programmers.co.kr/learn/courses/30/lessons/159994

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

 

 

[풀이]

 

쉽다. goal 을 기준으로 각각의 단어를 순회하면서 cards1 에 있으면 넘어가고, 없으면 cards2에 있는지 확인, 없으면 No / 다 검사해서 있으면 Yes 리턴하면 끝!

 

 

function solution(cards1, cards2, goal) {
    let index1 = 0;
    let index2 = 0;
    for (word of goal) {
        if(word === cards1[index1]) index1++;
        else if(word === cards2[index2]) index2++;
        else return "No";
    }
    return "Yes";
}

 

728x90
반응형