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

Zoo Da·2021년 11월 12일
0

프로그래머스

목록 보기
8/10
post-thumbnail

링크

https://programmers.co.kr/learn/courses/30/lessons/42577

sol1) Set

#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>

using namespace std;

bool solution(vector<string> phone_book) {
    bool answer = true;
    set<string> S;
    for(auto& c : phone_book) S.insert(c);
    for(auto& a : S){
        for(int i = 0; i < a.length(); i++){
            string temp = a.substr(0, i);
            if(S.count(temp)){
                answer = false;
                break;
            }
        }
    }
    return answer;
}
사고 과정

그냥 set을 쓰고... for(auto& c : S) 이런식으로 set을 순회하면서 c.substr()로 문자열의 길이만큼 쪼갠후에 해당 부분 문자열이 set안에 있다면
answer = false로 하고 break해주면 될 듯?

profile
메모장 겸 블로그

0개의 댓글