programmers.co.kr/learn/courses/30/lessons/42577
코딩테스트 연습 - 전화번호 목록
전화번호부에 적힌 전화번호 중, 한 번호가 다른 번호의 접두어인 경우가 있는지 확인하려 합니다. 전화번호가 다음과 같을 경우, 구조대 전화번호는 영석이의 전화번호의 접두사입니다. 구조
programmers.co.kr
길이대로 정렬 후 부분 비교를 진행한다.
#include <string>
#include <vector>
#include <algorithm>
#include <string>
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;
}
해쉬맵을 사용하는 방법도 있다.
#include <string>
#include <vector>
#include <unordered_map>
#include <string>
using namespace std;
unordered_map<string, int> um;
bool solution(vector<string> phone_book) {
bool answer = true;
int len = phone_book.size();
for(int i=0; i<len; i++){
um[phone_book[i]] = 1;
}
for(int i=0; i<len; i++){
string tmp = "";
for(int j=0; j<phone_book[i].size(); j++){
tmp += phone_book[i][j];
if(um[tmp] && tmp != phone_book[i]){
return false;
}
}
}
return answer;
}
'알고리즘 문제풀이 > 프로그래머스' 카테고리의 다른 글
[프로그래머스 2019 카카오 개발자 겨울 인턴십] 불량 사용자 (0) | 2021.01.19 |
---|---|
[프로그래머스 2019 카카오 개발자 겨울 인턴쉽] 튜플 (0) | 2021.01.19 |
[프로그래머스 2019 카카오 개발자 겨울 인턴십] 크레인 인형뽑기 게임 (0) | 2021.01.19 |
[프로그래머스] 레벨3 베스트앨범 (0) | 2021.01.19 |
[프로그래머스] 레벨2 위장 (0) | 2021.01.18 |
댓글