We use cookies (including Google cookies) to personalize ads and analyze traffic. By continuing to use our site, you accept our Privacy Policy.

Valid Phone Numbers

Number: 193

Difficulty: Easy

Paid? No

Companies: Amazon, Google, Media.net, Apple


Problem Description

Given a text file (file.txt) that lists phone numbers (one per line), print all valid phone numbers. A valid phone number appears in one of the following two formats:

  1. xxx-xxx-xxxx
  2. (xxx) xxx-xxxx where x represents a digit. Each line in the file does not contain any leading or trailing white spaces.

Key Insights

  • There are exactly two valid formats, which can be captured using a union of two regular expressions.
  • The regex should ensure the entire line is checked (using start and end anchors).
  • Using command-line tools like grep with extended regex (or egrep) provides a concise solution.
  • Input file constraints simplify the approach, as there are no extra spaces to worry about.

Space and Time Complexity

Time Complexity: O(n * m), where n is the number of lines and m is the average length of a line. Space Complexity: O(1) (excluding input storage), as we only store a few variables and perform line-by-line processing.


Solution

The approach is to use a regular expression that matches either of the two valid formats. For example, the bash one-liner uses grep with an extended regular expression that checks for:

  • Three digits, a hyphen, three digits, another hyphen, and four digits.
  • Or an opening parenthesis, three digits, a closing parenthesis, a space, three digits, a hyphen, and four digits. This one-liner can be written as: grep -E '^([0-9]{3}-[0-9]{3}-[0-9]{4}|([0-9]{3}) [0-9]{3}-[0-9]{4})$' file.txt Similarly, the idea can be extended to other programming languages by reading each line, using the same regex to match valid phone numbers, and printing the matching lines.

Code Solutions

Below are code solutions in various programming languages.

import re

# Compile the regex pattern that matches the valid phone number formats.
pattern = re.compile(r'^(\d{3}-\d{3}-\d{4}|\(\d{3}\) \d{3}-\d{4})$')

# Open and iterate over each line in the file.
with open('file.txt', 'r') as file:
    for line in file:
        line = line.strip()  # Remove any newline characters.
        if pattern.match(line):  # Check if the line matches the valid phone number pattern.
            print(line)
← Back to All Questions