euisblue
42577. 전화번호 목록

전화번호 목록

Solution

1#include <bits/stdc++.h> 2using namespace std; 3 4bool solution(vector<string> pb) { 5 sort(pb.begin(), pb.end()); 6 const int SIZE = pb.size(); 7 8 for(int i=0; i<SIZE-1; ++i) { 9 if(pb[i+1][0] != pb[i][0]) continue; 10 if(pb[i+1].find(pb[i]) != string::npos) 11 return false; 12 } 13 14 return true; 15}

사전 순 정렬 후, 인접한 문자열이 접두사인지 확인.

Time Complexity

O(N log N): sort 함수

Space Complexity

O(1)