코딩테스트

프로그래머스 전화번호 목록 c++

코앤미 2025. 2. 17. 19:33

 

포인트) 순서대로 정렬하면, 자신을 포함하는 번호는 무조건 자기 바로 옆에 온다.

따라서 정렬 후 자기 다음 수와 비교만 해보면 알 수 있다.

 

 

 

#include <string>
#include <vector>
#include<algorithm>
#include<iostream>
using namespace std;

//12343   123431 12342

bool isHead (string a, string b){
    for(int i=0;i<a.length();i++){
        if(a[i]!=b[i]) return false;
    }
    return true;
}

bool solution(vector<string> phone_book) {
    bool answer = true;
    sort(phone_book.begin(),phone_book.end());
    string prev =phone_book[0];

    for(int i=1;i<phone_book.size();i++){
        if(isHead(prev,phone_book[i])) return false;
        prev=phone_book[i];
    }
    // 전화 번호 1개인 경우엔 true
    return true;
}
















// #include <string>
// #include <vector>
// #include<map>
// #include<iostream>
// using namespace std;

// map<string,bool> ma;
// bool solution(vector<string> phone_book) {
//     bool answer = true;
//     for(int i=0;i<phone_book.size();i++) ma[phone_book[i]]=true;
    
    
//     for(int i=0;i<phone_book.size();i++){
        
        
//         string str="";
//         for(int len=0;len<phone_book[i].size()-1;len++){
//             str+=phone_book[i][len];
            
//             if(ma[str]) {
//                 answer=false;
              
//             }
//         }
       
//     }
        
        
  
    
//     return answer;
// }