[프로그래머스/C++]Lv.0 - 외계어 사전

YH J·2023년 4월 18일
0

프로그래머스

목록 보기
33/168

문제 링크

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

내 풀이

bool 변수를 하나 추가한뒤 spell의 글자 하나하나 dic의 원소에서 find해서 결과를 bool변수에 &연산 해줌

내 코드

#include <string>
#include <vector>

using namespace std;

int solution(vector<string> spell, vector<string> dic) {
    bool b;
    for(const auto& d : dic)
    {
        b = true;
        for(const auto& s : spell)
        {
            b &= d.find(s) != string::npos;
        }
        if(b)
            return 1;
    }
    
    return 2;
}

다른 사람의 풀이

#include <string>
#include <vector>

using namespace std;

int solution(vector<string> spell, vector<string> dic) {
    int answer = 2;

    for(const auto& v : dic)
    {
        bool test = true;
        for(const auto& t : spell)
        {
            if(v.find(t) == string::npos)
            {
                test= false;
                break;
            }
        }

        if(test)
        {
            answer = 1;
            break;
        }
    }
    return answer;
}

다른 사람의 풀이 해석

find해서 없으면 바로 break해서 멈춤. 탐색 횟수가 훨신 더 적을듯

profile
게임 개발자 지망생

0개의 댓글