[프로그래머스] 외계어 사전 - c++

삼식이·2025년 5월 7일
0

알고리즘

목록 보기
45/81

외계어 사전

C++에서 string의 find() 함수를 사용하여 문자열 내에서 특정 문자를 찾을 때, 찾는 문자열이 존재하지 않으면 find()는 string::npos를 반환한다. 이 값을 이용해 검색 실패 여부를 판단할 수 있다.

#include <string>
#include <vector>

using namespace std;

int solution(vector<string> spell, vector<string> dic) {
    int answer = 2;
    for(string s: dic) {
        int cnt = 0;
        for(string k: spell) {
            if (s.find(k) == string::npos) continue;
            cnt++;
        }
        if (cnt == spell.size()) {
            return 1;
        }
    }
    return answer;
}

0개의 댓글