You are given a string array numbers that represents phone numbers. Return true if no phone number is a prefix of any other phone number; otherwise, return false.
Example 1:
Input: numbers = ["1","2","4","3"]
Output: true
Explanation:
No number is a prefix of another number, so the output is true.
Example 2:
Input: numbers = ["001","007","15","00153"]
Output: false
Explanation:
The string "001" is a prefix of the string "00153". Thus, the output is false.
Constraints:
2 <= numbers.length <= 501 <= numbers[i].length <= 50'0' to '9'.Problem Overview: You receive a list of phone numbers. The task is to determine whether any number is a prefix of another number in the list. For example, if 911 and 9112345 both appear, the list is invalid because one number starts with another.
Approach 1: Brute Force Pair Comparison (O(n² * m) time, O(1) space)
Compare every phone number with every other phone number. For each pair, check whether one string starts with the other using a prefix comparison such as startsWith. If any match appears, return false immediately. This approach is easy to reason about but scales poorly because it checks n² pairs. With long phone numbers of length m, each comparison adds additional cost. This works only when the list size is very small.
Approach 2: Sorting + Prefix Checking (O(n log n * m) time, O(1) extra space)
Sort the phone numbers lexicographically using standard sorting. After sorting, any numbers that share a prefix will appear next to each other. Iterate through the sorted list once and compare each element with the next element. If numbers[i+1] starts with numbers[i], you found a prefix conflict. The key insight is that sorting groups similar prefixes together, eliminating the need for comparing every pair. This approach is simple, efficient, and widely used in interviews. The main operations are sorting the array and performing linear prefix checks using string operations.
Approach 3: Trie (Prefix Tree) (O(n * m) time, O(n * m) space)
Insert each phone number digit by digit into a trie. While inserting, check two conditions: if you reach a node already marked as the end of another number, or if the current number finishes but the node already has children. Either condition means one number is a prefix of another. Tries provide optimal lookup for prefix relationships and guarantee linear processing relative to total digits. The tradeoff is higher memory usage and more implementation overhead compared to sorting.
Recommended for interviews: Sorting + prefix checking is usually the expected answer. It shows you recognize that prefix conflicts must appear among neighboring elements after sorting. Mentioning the brute force approach demonstrates baseline reasoning, while presenting the sorted optimization shows algorithmic thinking. A trie solution is also strong when the interviewer specifically wants a prefix data structure or asks how to scale the solution for extremely large datasets.
We can first sort the array numbers based on the length of strings. Then, we iterate through each string s in the array and check if there is any previous string t that is a prefix of s. If such a string exists, it means there is a string that is a prefix of another string, so we return false. If we have checked all strings and haven't found any prefix relationships, we return true.
The time complexity is O(n^2 times m + n times log n), and the space complexity is O(m + log n), where n is the length of the array numbers, and m is the average length of strings in the array numbers.
Python
Java
C++
Go
TypeScript
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Pair Comparison | O(n² * m) | O(1) | Small input sizes or quick prototype solutions |
| Sorting + Prefix Checking | O(n log n * m) | O(1) extra | Most practical solution; simple implementation with good performance |
| Trie (Prefix Tree) | O(n * m) | O(n * m) | Large datasets or problems focused on prefix lookups |
Practice Phone Number Prefix with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor