호지

[프로그래머스] 카드 뭉치 문제풀이 JS 본문

알고리즘/프로그래머스

[프로그래머스] 카드 뭉치 문제풀이 JS

_hoji

goal에 있는 모든 단어를 2개의 카드 뭉치에서 찾을 수 있는지 확인하는 문제이다.

cards1에서 다음단어가 찾는 단어가 맞으면, pos1을 1증가,

cards2에서 다음단어가 찾는 단어가 맞으면, pos2를 1증가,

이 외의 경우엔 단어를 못찾은 것이므로, answer은 "No"가 된다.

function solution(cards1, cards2, goal) {
  var answer = "Yes";
  let pos1 = -1,
    pos2 = -1;

  for (const g of goal) {
    if (cards1[pos1 + 1] === g) pos1++;
    else if (cards2[pos2 + 1] === g) pos2++;
    else {
      answer = "No";
      break;
    }
  }
  return answer;
}

 

Comments