[프로그래머스] 대충 만든 자판

정채은·2025년 7월 23일

C++ 프로그래밍

목록 보기
14/16

❓문제 : 대충 만든 자판


✨나의 풀이

#include <string>
#include <vector>
#include <unordered_map>
#include <algorithm>

using namespace std;

vector<int> solution(vector<string> keymap, vector<string> targets) {
    vector<int> answer;
    unordered_map<char, int> keyPress;
    
     for (const string& key : keymap) {
        for (int i = 0; i < key.size(); ++i) {
            char ch = key[i];
            //키가 존재하는지 확인
            if (keyPress.find(ch) == keyPress.end()) {
                keyPress[ch] = i + 1;
            } 
            else { // 없으면 작은 값 비교해서 값 업데이트
                keyPress[ch] = min(keyPress[ch], i + 1);
            }
        }
    }
    
    for (const string& target : targets){
        int total = 0;
        
        for (char ch : target) {
            if (keyPress.find(ch) == keyPress.end()) {
                total = -1;
                break;
            } 
            else {
                total += keyPress[ch];
            }
        }
        answer.push_back(total);
    }
    
    return answer;
}

profile
누군가에게 추억을 만들어주는 그날까지

0개의 댓글