Skip to main content

Phone Number Prefix - Solution & Explanation

EasyPremiumFree on FleetCodeArrayStringTrieSorting7 min read
Practice this problem

Problem Statement

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 <= 50
  • 1 <= numbers[i].length <= 50
  • All numbers contain only digits '0' to '9'.

Approach Overview

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.

Solution

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.

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force Pair ComparisonO(n² * m)O(1)Small input sizes or quick prototype solutions
Sorting + Prefix CheckingO(n log n * m)O(1) extraMost practical solution; simple implementation with good performance
Trie (Prefix Tree)O(n * m)O(n * m)Large datasets or problems focused on prefix lookups

Video Solution

leetcode 3491 Phone Number Prefix | prefix tree trie, compact logic into one succinct function • Code-Yao • 103 views views

Frequently Asked Questions

Is Phone Number Prefix easy or hard?
Phone Number Prefix is generally considered an easy problem. The main insight is recognizing that sorting places potential prefix pairs next to each other, allowing a single linear scan instead of comparing every pair.
Phone Number Prefix Python/Java solution
Most implementations sort the list of phone numbers and compare each number with the next one using a prefix check such as startswith in Python or startsWith in Java. This keeps the logic simple and runs in O(n log n * m) time.
How to solve Phone Number Prefix in O(n)?
A trie (prefix tree) can detect prefix conflicts in O(n * m) time, where n is the number of phone numbers and m is the maximum length. Insert each digit into the trie while checking if a node already marks the end of another number or has existing children. Either condition indicates a prefix relationship.
What is the best approach for Phone Number Prefix?
Sorting the phone numbers and checking adjacent pairs is the most practical solution. After lexicographic sorting, numbers with shared prefixes appear next to each other, so a single linear scan detects conflicts. The approach runs in O(n log n * m) time with O(1) extra space and is easy to implement in interviews.
Is Phone Number Prefix asked at Google/Amazon/Meta?
Prefix-based string problems appear frequently in interviews at companies like Amazon, Google, and Meta. Variations often involve prefix validation, dictionary lookups, or trie-based searching. The problem tests understanding of sorting strategies and prefix data structures.
What data structure is used in Phone Number Prefix?
The problem can be solved using arrays and string comparisons with sorting, which is the simplest approach. A trie (prefix tree) is another common data structure for detecting prefix relationships efficiently when processing many strings.
What is the time complexity of Phone Number Prefix?
The common optimized solution uses sorting followed by a linear scan. Sorting n phone numbers costs O(n log n), and each prefix comparison takes O(m) for numbers of length m, giving O(n log n * m) total time. Space complexity is typically O(1) beyond the input array.

Ready to solve this problem?

Practice Phone Number Prefix with our built-in code editor and test cases.

Practice on FleetCode