[프로그래머스 | Javascript] 카드 뭉치

박기영·2023년 2월 21일
1

프로그래머스

목록 보기
121/159

solution

function checkCards (cards, goal) {
    const cardsFilteredByGoal = goal.filter((item, index) => cards.includes(item));
    
    const cardFilteredByIndex = cardsFilteredByGoal.filter((item, index) => item === cards[index]);
    
    if(cardFilteredByIndex.length !== cardsFilteredByGoal.length){
        return false;
    }
    
    return true;
}

function solution(cards1, cards2, goal) {    
    if(checkCards(cards1, goal) && checkCards(cards2, goal)){
        return "Yes";
    }
    
    return "No";
}

필자는 함수를 만들어서 풀었다. 함수를 설명하자면 다음과 같다.

목표 문장에서 하나의 카드(ex. cards1)에 들어있는 애들만 쭉 뽑아낸다.
이는 문제 조건에 cards1, cards2가 다른 단어로만 이루어져있다고 명시되어 있기 때문에 가능하다.

뽑아낸 배열에서 해야할 것은 순서를 보는 것이다.
단순 포함이 아니라, 순서까지 똑같은 상태로 포함이 되어 있어야 정답이기 때문이다.
따라서, 뽑아낸 배열에서 filter()를 사용하여 대상 카드(ex. cards1)의 값들과 인덱스까지 일치하는지 판별한다.

만약, 모든 조건을 만족했다면 위에서 처리한 두 배열의 길이가 같을 것이다.
다르다면 대상 카드(ex. cards1)는 정답이 될 수 없으므로 false, 같다면 정답이므로 true를 반환한다.

이를 cards1, cards2에 대해 각각 진행해준다.
두 카드가 모두 정답이 되는 경우에만 문제를 통과할 수 있으므로, Yes를 반환하고,
하나라도 정답이 아닌 경우라면 문제를 통과할 수 없으므로, No를 반환한다.

다른 분 풀이

function solution(cards1, cards2, goal) {

    for(const s of goal) {

        if(cards1[0] == s) {
            cards1.shift();
        } else if(cards2[0] == s) {
            cards2.shift();
        } else {
            return "No"
        }
    }

    return "Yes";
}

다른 분들은 정말 코드가 간단했다.
goal에 있는 문자열을 하나씩 판별하는 방법으로,
cards1에 해당하는 문자라면 cards1에서 제거하고, cards2에 해당하는 문자라면 cards2에서 제거한다.
만약, 그 어느 쪽에도 해당하지 않는다면 이는 없는 단어거나, 순서가 맞지 않는 단어이므로 No.
이 모든 과정을 통과하면 순서까지 만족하는 경우이므로 Yes.

profile
나를 믿는 사람들을, 실망시키지 않도록

15개의 댓글

comment-user-thumbnail
2024년 8월 1일

와 다른사람 풀이 진짜 대박이네요..!따봉

답글 달기
comment-user-thumbnail
2025년 7월 30일

This is often as a result exquisite and even very creative. Freezing take pleasure in that tones and even whomever makes the application with the -mail can be beaming. halloween sexy

답글 달기
comment-user-thumbnail
2025년 8월 4일

Fantastic submit, Many thanks regarding revealing This kind of information. Fantasticly composed write-up, only if almost all people supplied the identical amount of articles when you, the net would have been a greater spot. You should keep writing! https://www.cricketweb.net/1win-promo-code-welcome-bonus-500-up-to-1025/

답글 달기
comment-user-thumbnail
2025년 8월 4일

Stick to the fantastic job, That i look over couple articles or blog posts on this subject web-site together with It is my opinion that online webpage is certainly legitimate appealing and features have communities for superb material. night club istanbul

답글 달기
comment-user-thumbnail
2025년 8월 5일

There exist a couple of many different distinct levels among the California Weight loss program and each and every a person is pretty important. You’re procedure stands out as the the actual giving up with all the power. weight loss Niche Edit/Link Insertion

답글 달기
comment-user-thumbnail
2025년 8월 6일

정말 멋진 블로그네요! 이 블로그는 모든 면에서 정말 훌륭합니다. 특히 디자인이 멋지고, 콘텐츠의 질도 최고입니다. 가락시장 노래방

답글 달기
comment-user-thumbnail
2025년 8월 17일

No doubt this is an excellent post I got a lot of knowledge after reading good luck. Theme of blog is excellent there is almost everything to read, Brilliant post. skinpres t

답글 달기
comment-user-thumbnail
2025년 8월 18일

Took me time to read all the comments, but I really enjoyed the article. It proved to be Very helpful to me and I am sure to all the commenters here! It’s always nice when you can not only be informed, but also entertained! pol88 login

답글 달기
comment-user-thumbnail
2025년 8월 18일

Took me time to read all the comments, but I really enjoyed the article. It proved to be Very helpful to me and I am sure to all the commenters here! It’s always nice when you can not only be informed, but also entertained! situs mawartoto

답글 달기
comment-user-thumbnail
2025년 8월 24일

Thanks for your insight for your fantastic posting. I’m glad I have taken the time to see this.situs toto slot

답글 달기
comment-user-thumbnail
2025년 8월 25일

Positive site, where did u come up with the information on this posting? I'm pleased I discovered it though, ill be checking back soon to find out what additional posts you include. agentotoplay

I am very happy to discover your post as it will become on top in my collection of favorite blogs to visit. duit66

I havent any word to appreciate this post.....Really i am impressed from this post....the person who create this post it was a great human..thanks for shared this with us. Gsc108

답글 달기
comment-user-thumbnail
2025년 8월 28일

I am very happy to discover your post as it will become on top in my collection of favorite blogs to visit. click for source

답글 달기
comment-user-thumbnail
2025년 9월 2일

Yes, I am entirely agreed with this article, and I just want say that this article is very helpful and enlightening. I also have some precious piece of concerned info !!!!!!Thanks. delhi satta king

답글 달기
comment-user-thumbnail
2025년 9월 3일

Pretty good post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I'll be subscribing to your feed and I hope you post again soon. Big thanks for the useful info. sem services

답글 달기
comment-user-thumbnail
2025년 9월 3일

i really like this article please keep it up.[https://www.aimbit.com/](it was a wonderful chance to visit this kind of site and I am happy to know. thank you so much for giving us a chance to have this opportunity..)Voxcoin

답글 달기