• Home
  • About
    • Jiwon Jeong photo

      Jiwon Jeong

      끊임없이 배우며 성장하는 엔지니어

    • Learn More
    • Email
    • Github
  • Posts
    • All Posts
    • All Tags
    • All Categories
  • Projects

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

08 May 2021

Reading time ~1 minute

풀이 1

#include <string>
#include <vector>

using namespace std;

bool solution(vector<string> phone_book) {
    bool answer = true;
    sort(phone_book.begin(), phone_book.end());
    
    for(string s1: phone_book){
        for(string s2: phone_book){
            if(s1==s2)
                continue;
            if(s1.length()>s2.length()){
                if(s2==s1.substr(0,s2.length())){
                    return false;
                }
            }
        }
    }
    return answer;

    
}

풀이 2

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

bool solution(vector<string> phone_book) {
    bool answer = true;
    sort(phone_book.begin(), phone_book.end());
    
    for(int i=0; i<phone_book.size()-1; i++){
       if(phone_book[i]==phone_book[i+1].substr(0, phone_book[i].size())){
           return false;
       }
        
    }
    return answer;

}



PS Share Tweet +1