hash 사용할 때 sort 활용
비교할때는 배열의 개수 확인
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
bool solution(vector<string> phone_book) {
sort(phone_book.begin(), phone_book.end());
for(int i=0;i<phone_book.size()-1;i++){
if(phone_book[i+1].substr(0, phone_book[i].size())==phone_book[i])
return false;
}
return true;
}
unordered_set을 사용하여 phone_book string으로 저장
string s ="";
[i][j]하나씩 추가하면서 같은 문자열이 있는지 찾기
uset.find(s) && 자기자신은 안됨
#include <iostream>
#include <vector>
#include <unordered_set>
using namespace std;
unordered_set<string> uset;
bool solution(vector<string> phone_book) {
bool answer = true;
for (auto num : phone_book)
uset.insert(num);
int bookSize = phone_book.size();
for (int i = 0; i < bookSize; i++) {
string s = "";
int numSize = phone_book[i].size();
for (int j = 0; j < numSize; j++) {
s += phone_book[i][j];
if (uset.find(s)!=uset.end() && phone_book[i].compare(s) != 0) {
answer = false;
}
}
}
return answer;
}
#include <iostream>
#include <vector>
#include <unordered_set>
using namespace std;
unordered_set<string> uset;
bool solution(vector<string> phone_book) {
bool answer = true;
for (auto num : phone_book)
uset.insert(num);
int bookSize = phone_book.size();
for (int i = 0; i < bookSize; i++) {
string s = "";
int numSize = phone_book[i].size();
for (int j = 0; j < numSize; j++) {
s += phone_book[i][j];
if (uset.find(s)!=uset.end() && phone_book[i].compare(s) != 0) {
answer = false;
}
}
}
return answer;
}
int main(void)
{
vector<string> phone_book = { "123","456","789" };
cout << solution(phone_book);
return 0;
}