본문 바로가기

problem solving

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

중복을 찾으면 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<stringbool> 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

'problem solving' 카테고리의 다른 글

[프로그래머스]괄호 변환  (0) 2020.03.10
[프로그래머스]숫자 야구  (0) 2020.03.10
[프로그래머스]H-index  (0) 2020.03.10
[프로그래머스]더 맵게  (0) 2020.03.10
[프로그래머스]소수 찾기  (0) 2020.03.10