problem solving
[프로그래머스]전화번호 목록
Shinuk Yi
2020. 3. 10. 18:26
중복을 찾으면 false를 반환하도록했다.
처음에 정렬을 하는 이유는
1234, 123, 12와 같이 앞의 수가 더 길다면 비교할 때 접두어가 다르기 때문이다.
#include <string>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;
bool solution(vector<string> phone_book) {
sort(phone_book.begin(), phone_book.end());
map<string, bool> mainMap;
for (int idx = 0; idx < phone_book.size(); idx++) {
string str = "";
for(int i = 0; i < phone_book[idx].length(); i++) {
str += phone_book[idx][i];
if(mainMap.find(str) != mainMap.end()) {
return false;
}
}
mainMap[phone_book[idx]] = true;
cout << phone_book[idx] << "\n";
}
return true;
}
|
cs |