Created
February 24, 2021 05:13
-
-
Save DongguemYoo/6c052879698064d55149bf1d8a02f774 to your computer and use it in GitHub Desktop.
[코딩테스트]전화번호 목록(feat 해쉬)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### 내풀이 ### | |
## 길이를 측정해서 길이만큼만 비교## | |
def solution(phone_book): | |
answer = True | |
dict = {} | |
for item in phone_book: | |
dict.update({item:item}) | |
for item in dict.keys(): | |
checker = item | |
for tester in dict.keys(): | |
if tester == checker: | |
continue | |
lenght = len(checker) | |
target_lenght = len(tester) | |
# print("tester%s"%tester) | |
if lenght<=target_lenght: | |
# print("checker[:lenght]%s ,tester[:lenght]%s" % (checker[:lenght], tester[:lenght])) | |
if checker[:lenght] in tester[:lenght]: | |
# print("checker%s 가tester%s에 들어가있습니다"%(checker[:lenght],tester[:lenght])) | |
return False | |
# print("체커:%s상대길이:%s" %(checker ,target_lenght) ) | |
return answer | |
# phone_book = ['119', '97674223', '1195524421'] | |
phone_book = ['113', '44', '4544'] | |
# phone_book = ['113', '44', '4544'] | |
solution(phone_book) | |
########출제위원이 원하는 풀이##### | |
####해쉬를 사용해서 풀이#### | |
def solution(phone_book): | |
answer = True | |
hash_map = {} | |
for phone_number in phone_book: | |
hash_map[phone_number] = 1 | |
for phone_number in phone_book: | |
temp = "" | |
for number in phone_number: | |
temp += number | |
if temp in hash_map and temp != phone_number: | |
answer = False | |
return answer | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment