본문 바로가기

프로그래머스 문제풀이/LEVEL 2

[C++] 프로그래머스 문제풀이 LEVEL 2 스킬트리

 

 

https://school.programmers.co.kr/learn/courses/30/lessons/49993?language=cpp 

 

프로그래머스

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

programmers.co.kr

 

 

 

#include <bits/stdc++.h>


bool can(std::unordered_map<char, int> map, std::string skill, std::string str) {
    int index = 0;
    for (int i = 0; i < str.length(); ++i) {
        char ch = str[i];
        if (map.find(ch) != map.end()) {
            if (index == skill.length())
                return true;
            if (skill[index] == ch) {
                ++index;
            } else {
                return false;
            }
        }
    }
    return true;
}

int solution(std::string skill, std::vector<std::string> skill_trees) {
    int answer = 0;
    std::unordered_map<char, int> map;
    for (int i = 0; i < skill.length(); ++i) {
        char ch = skill[i];
        map[ch] = i;
    }

    for (std::string str : skill_trees) {
        if (can(map, skill, str)) {
            ++answer;
        }
    }
    return answer;
}