[문제 링크]

 

코딩테스트 연습 - 전화번호 목록

전화번호부에 적힌 전화번호 중, 한 번호가 다른 번호의 접두어인 경우가 있는지 확인하려 합니다. 전화번호가 다음과 같을 경우, 구조대 전화번호는 영석이의 전화번호의 접두사입니다. 구조��

programmers.co.kr


간단한 문자열 검색 문제였다.

 

입력받은 전화번호 목록을 오름차순 정렬한 뒤 이중 for문을 돌려 어떤 번호를 접두어로 받는 번호가 있다면 false를 반환하도록 구현하였다.


#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].length()))
				return false;

	return true;
}

알고리즘 200일 프로젝트 - 135 day

+ Recent posts