[백준] 5052번 전화번호 목록

HL·2021년 1월 17일
0

백준

목록 보기
17/104
  • 출처 : https://www.acmicpc.net/problem/5052

  • 알고리즘 : 정렬

  • 풀이방법

    • 문자열 정렬
    • i, i+1 번째 값들을 비교
    • i+1 startswith i 이면 NO, 아니면 YES
  • 소감

    • 알고리즘 분류가 정렬에 들어가있어서 아이디어가 떠오를 수 밖에 없었다
    • 프로그래머스에서 풀었던 문제인데 그때는 HashMap을 이용해서 더 비효율적으로 푼 것 같다

코드

# 백준

import sys

testcase = int(input(''))

for t in range(testcase):

    pn_list = []

    num = int(input(''))
    for n in range(num):

        phone_number = sys.stdin.readline().rstrip()
        pn_list.append(phone_number)
    
    pn_list.sort()

    result = True
    for i in range(num-1):

        if pn_list[i+1].startswith(pn_list[i]):
            result = False
            break

    if result:
        print('YES')
    else:
        print('NO')
profile
Swift, iOS 앱 개발을 공부하고 있습니다

0개의 댓글