Phone Number Prefix - Problem

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.

A prefix is a substring that appears at the beginning of another string. For example, "123" is a prefix of "12345", but "234" is not a prefix of "12345".

Input & Output

Example 1 — Has Prefix
$ Input: numbers = ["123", "12", "456"]
Output: false
💡 Note: "12" is a prefix of "123", so return false
Example 2 — No Prefix
$ Input: numbers = ["123", "456", "789"]
Output: true
💡 Note: No number is a prefix of any other number
Example 3 — Single Digit Prefix
$ Input: numbers = ["1", "12", "123"]
Output: false
💡 Note: "1" is a prefix of both "12" and "123"

Constraints

  • 1 ≤ numbers.length ≤ 1000
  • 1 ≤ numbers[i].length ≤ 20
  • numbers[i] consists of digits only

Visualization

Tap to expand
Phone Number Prefix Detection"123""12""456"Check: Is "12" a prefix of "123"?PREFIX!Output: false"12" is the beginning of "123"
Understanding the Visualization
1
Input
Array of phone number strings
2
Process
Check if any number is prefix of another
3
Output
Return true if no prefix exists, false otherwise
Key Takeaway
🎯 Key Insight: Sort the array first - prefixes will always be adjacent to their longer versions
Asked in
Google 45 Amazon 38 Microsoft 32
23.5K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen