본문 바로가기
스터디/알고리즘

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

by 알 수 없는 사용자 2020. 2. 11.

사용 언어 : C++

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <string>
#include <vector>
#include <algorithm>
#include <iostream>
 
using namespace std;
 
bool length_sort(const string a, const string b)
{
    return a.length() < b.length();
}
 
bool solution(vector<string> phone_book) {
 
    sort(phone_book.begin(), phone_book.end(), length_sort); // 길이로 desc
    string temp;
 
    for (int i = 0; i < phone_book.size(); ++i)
    {
        for (int j = i + 1; j < phone_book.size(); ++j)
        {
            temp = phone_book[j].substr(0, phone_book[i].size());
 
            if (!temp.compare(phone_book[i]))
                return false;
        }
    }
    return true;
}