๐ŸŒฑ → ๐ŸŒณ

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์ „ํ™”๋ฒˆํ˜ธ ๋ชฉ๋ก - python ๋ณธ๋ฌธ

Algorithms

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์ „ํ™”๋ฒˆํ˜ธ ๋ชฉ๋ก - python

thals0 2023. 8. 29. 09:59
728x90

๋ฌธ์ œ ์ •๋ณด 

https://school.programmers.co.kr/learn/courses/30/lessons/42577

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr

 

์ฝ”๋“œ

์‹œ๊ฐ„ ์ดˆ๊ณผ ๋ฐ ๋ช‡๊ฐœ์˜ ํ…Œ์ŠคํŠธ์—์„œ ์‹คํŒจ

def solution(phone_book):
    answer = True
    for i in range(len(phone_book)):
        for j in range(i+1, len(phone_book)):     
            if phone_book[i] in phone_book[j]:
                answer = False
    return answer

→ ๋ฐ˜๋ก€ ์ฐพ์•„๋ณด๊ธฐ

((์ˆ˜์ •))

์š”๊ณ  ์ •๋ ฌํ•˜๋ฉด ๋จ 

["1234", "12"] ์ผ๋•Œ๋ฅผ ์ฒดํฌ ๋ชปํ•จ 

์ •๋ ฌํ•˜๋ฉด ["12", "1234"] ์—ฌ์„œ ์ฒดํฌ ๊ฐ€๋Šฅ 

 

์ •๋‹ต์ฝ”๋“œ 

def solution(phone_book):
    answer = True
    phone_book.sort()
    for i in range(len(phone_book)-1):
        if phone_book[i] == phone_book[i+1][:len(phone_book[i])]:
            answer = False
            break
    return answer
def solution(phoneBook):
    phoneBook = sorted(phoneBook)

    for p1, p2 in zip(phoneBook, phoneBook[1:]):
        if p2.startswith(p1):
            return False
    return True

 

728x90