
좀 더 모듈화를 많이 해야 가독성이 더 좋아질 거 같다.
#include <string>
#include <vector>
#include <iostream>
#include <climits>
using namespace std;
//더 적은 클릭으로 타겟 문자를 찾을 수 있는 경우 찾기
int find_key(char current_word, vector<string> &keymap){
int min_count = INT_MAX, count = 0;
int i, j;
for(i = 0; i < keymap.size(); i++){
count = 0;
for(j = 0; j < keymap[i].size(); j++){
count++;
if(current_word == keymap[i][j]){
min_count = min(min_count, count);
break;
}
}
}
return (min_count == INT_MAX) ? 0 : min_count;
}
vector<int> solution(vector<string> keymap, vector<string> targets) {
vector<int> answer;
int i, j;
char current_word;
string current_string;
int sum = 0, count = 0;
for(i = 0; i < targets.size(); i++){
sum = 0;
current_string = targets[i];
cout << "current_string : " << current_string << "\n";
for(j = 0; j < current_string.size(); j++){
current_word = current_string[j];
cout << "current_word : " << current_word << " -> ";
count = find_key(current_word, keymap);
if(count == 0){
break;
}
else{
sum += count;
cout << count << "\n";
}
}
cout << "sum : " << sum << "\n";
//if(sum == 0) -> 이렇게 적으면 첫 문자 찾고 그 다음 문자 못 찾았을 때 -1을 못하게 됨
if(count == 0){
answer.push_back(-1);
}
else{
answer.push_back(sum);
}
}
return answer;
}