https://programmers.co.kr/learn/courses/30/lessons/42577
Solution
This was categorized as a hash problem, but I solved it with sorting and index comparison. Since n isn’t particularly small, brute-force comparison of all pairs isn’t feasible.
- Sort the phonebook list.
- Compare the i-th and (i+1)-th phone numbers. Assuming i is the potential prefix, check the front portion of i+1.
- If they match, set answer=False.
Two approaches for step 2:
# 1. Direct prefix comparison using indexingif phone_book[i + 1][:len(phone_book[i])] == phone_book[i]
# 2. Using startswithif phone_book[i + 1].startswith(phone_book[i])Code
https://github.com/naem1023/codingTest/blob/master/pg-30-42577.py