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:
- xxx-xxx-xxxx
- (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.