본문 바로가기
알고리즘/프로그래머스

[ 프로그래머스 / map ] 전화번호 목록

by 뎁꼼 2020. 6. 30.

1. 문제


 

 

코딩테스트 연습 - 전화번호 목록

전화번호부에 적힌 전화번호 중, 한 번호가 다른 번호의 접두어인 경우가 있는지 확인하려 합니다. 전화번호가 다음과 같을 경우, 구조대 전화번호는 영석이의 전화번호의 접두사입니다. 구조��

programmers.co.kr

 

 

2. 소스코드


- map을 이용해서, 해당 현재 문자열의 앞부분이 존재하는지 확인해주면 된다.

 

소스코드

#include <string>
#include <vector>
#include <unordered_map>
using namespace std;
unordered_map<string,int> map;
bool solution(vector<string> phone_book) {
    for(string str : phone_book)
        map[str] = 1;
    
    int size = phone_book.size();
    for(int i = 0; i < size; ++i){
        string prefix = "";
        for(int j = 0; j < phone_book[i].size(); ++j){
            prefix += phone_book[i][j];
            if(map[prefix] and prefix != phone_book[i])
                return false;
        }
    }
    return true;
}