top of page

Coding Test] 전화번호 목록 문제

  • 작성자 사진: 김영호
    김영호
  • 2023년 5월 23일
  • 1분 분량
  • Code

#include <string>
#include <vector>
#include <algorithm>

using namespace std;

bool solution(vector<string> phone_book) {
    //정렬
    sort(phone_book.begin(), phone_book.end());
    //0 ~ phone_book.size() - 1
    for(int i = 0; i < phone_book.size()-1; i++)
    {
        //다음 string의 접두어가 이번 string임
        if(phone_book[i+1].find(phone_book[i]) == 0)
                return false;
    }
    return true;
}

string을 sort하게 되면 ASCII 코드 순으로 정렬된다.

그러므로 비슷한 string들끼리 모이게 되는 효과가 생기게 되고, i+1번째 string에서 i번째 string을 find() 함수로 찾았을때 그 위치가 0이면 접두어라는 뜻이므로 문제에서 원하는대로 false를 반환한다.

관련 게시물

  • Facebook
  • Twitter
  • LinkedIn

©2021 by 김영호_포트폴리오. Proudly created with Wix.com

bottom of page