point
문자열의 접두어가 존재하는지 확인하는 문제. 반복을 최소화 하는 것이 관건!
시간 초과
def solution(phone_book):
answer = True
phone_len_book = [(phone, len(phone)) for phone in phone_book]
phone_len_book.sort(key=lambda x: x[0])
phone_len_book.sort(key=lambda x: x[1])
i = 0
while i < len(phone_book)-1 and answer:
head = phone_len_book[i][0]
head_len = phone_len_book[i][1]
for phone in phone_len_book[i+1:]:
if phone[0][:head_len] == head:
answer = False
break
i += 1
return answer
def solution(phone_book):
answer = True
phone_len = [len(phone) for phone in phone_book]
min_len = min(phone_len)
max_len = max(phone_len)
phone_book_len = len(phone_book)
for i in range(min_len, max_len):
new_phones = [phone[:i] for phone in phone_book]
if len(set(new_phones)) != phone_book_len:
answer= False
break
return answer
def solution(phone_book):
answer = True
phone_book.sort()
for i in range(len(phone_book)-1):
if phone_book[i+1].startswith(phone_book[i]):
answer= False
break
return answer
풀이 3을 참고해서 풀이 1을 수정한 풀이
def solution(phone_book):
answer = True
phone_book.sort()
for i in range(len(phone_book)-1):
if phone_book[i+1][:len(phone_book[i])] == phone_book[i]:
answer = False
break
return answer