●문제

●입출력

사용알고리즘 : Hash / unordered_map
푼방법 : unordered_map<string, int>로 전화번호부를 emplace함수로 전부다 대입후
접두사 후보를 substr로 길이만큼 잘라 만든뒤
.find함수로 map에서 찾는다
찾았으면 false 없으면 true 반환
#include <string>
#include <vector>
#include <unordered_map>
using namespace std;
bool solution(vector<string> phone_book)
{
unordered_map<string, int> myMap;
//전화번호부 map에 다 넣기
for (int i = 0; i < phone_book.size(); ++i)
{
myMap.emplace(phone_book[i], 1);
}
//접두사 확인하기
for (int i = 0; i < phone_book.size(); ++i)
{
//같은번호
string entireNum = phone_book[i];
for (int j = 0; j < entireNum.size(); ++j)
{
string prefix = entireNum.substr(0, j);
if (myMap.find(prefix) != myMap.end())
{
return false;
}
}
}
return true;
}'C++ 프로그래머스 > Hash' 카테고리의 다른 글
| 프로그래머스(C++) - 의상 (1) | 2025.08.11 |
|---|---|
| 프로그래머스(C++) - 완주하지 못한 선수 (0) | 2025.08.08 |
| Algorithm - Hash (0) | 2025.08.08 |
