sort 이용해서 쉽게 풀기
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
bool solution(vector<string> phone_book) {
bool answer = true;
sort(phone_book.begin(), phone_book.end());
for(int i = 0; i < phone_book.size()-1; i++){
string curr = phone_book[i];
string next = phone_book[i+1];
if(curr == next.substr(0,curr.size())) return false;
}
return answer;
}
해쉬 이용해서 정석대로 풀기
#include <string>
#include <vector>
#include <unordered_map>
using namespace std;
bool solution(vector<string> phone_book) {
bool answer = true;
unordered_map<string,int> hash_map;
for(int i = 0; i < phone_book.size(); i++)
hash_map[phone_book[i]] = 1;
for(int i = 0; i < phone_book.size(); i++){
string curr = "";
for(int j = 0; j < phone_book[i].size(); j++){
curr += phone_book[i][j];
if(hash_map[curr] && curr != phone_book[i]) return false;
}
}
return true;
}