Given a text file file.txt that contains a list of phone numbers (one per line), write a one-liner bash script to print all valid phone numbers.
You may assume that a valid phone number must appear in one of the following two formats: (xxx) xxx-xxxx or xxx-xxx-xxxx. (x means a digit)
You may also assume each line in the text file must not contain leading or trailing white spaces.
Example:
Assume that file.txt has the following content:
987-123-4567 123 456 7890 (123) 456-7890
Your script should output the following valid phone numbers:
987-123-4567 (123) 456-7890
Problem Overview: You receive a text file where each line may contain a phone number. Print only the lines that match valid U.S. formats: xxx-xxx-xxxx or (xxx) xxx-xxxx. Any line that does not strictly follow one of these patterns must be ignored.
Approach 1: Regular Expression Matching (O(n * m) time, O(1) space)
The most direct solution uses a regular expression to validate the exact structure of each line. The regex anchors the pattern with ^ and $ so partial matches are rejected. The two valid formats are captured using alternation: ^([0-9]{3}-[0-9]{3}-[0-9]{4}|\([0-9]{3}\) [0-9]{3}-[0-9]{4})$. In Shell, tools like grep -E, awk, or sed iterate through each line and print matches. The algorithm scans every line once (n lines, m characters per line), making the runtime O(n * m) with constant auxiliary space.
Approach 2: Manual Pattern Validation (O(n * m) time, O(1) space)
In languages such as Python, Java, C++, or JavaScript, you can validate the format manually without regex. Iterate through each line and check its structure character by character. For the xxx-xxx-xxxx format, confirm digits at indices 0–2, 4–6, and 8–11, and hyphens at indices 3 and 7. For the (xxx) xxx-xxxx format, check parentheses at indices 0 and 4, a space at index 5, and hyphens at index 9 while verifying digits in the remaining positions. This method uses basic string indexing and conditional checks. Time complexity remains O(n * m) because every character may be inspected once.
Recommended for interviews: The regex approach is what interviewers typically expect for the Shell version of the problem. It demonstrates familiarity with pattern matching tools like grep and shows you can encode strict format constraints concisely. Implementing manual validation helps show deeper understanding of string parsing, but regex is the fastest and most idiomatic solution for this task.
Solutions for this problem are being prepared.
Try solving it yourself| Approach | Time | Space | When to Use |
|---|---|---|---|
| Regular Expression Matching (grep/awk/sed) | O(n * m) | O(1) | Best for Shell scripting and concise validation of structured text patterns |
| Manual Pattern Validation (String Checks) | O(n * m) | O(1) | Useful in general-purpose languages when regex is unavailable or clarity is preferred |
How many LeetCode problems should you solve? #leetcode #techinterview #developer #softwareengineer • CrioDo • 304,593 views views
Watch 9 more video solutions →Practice Valid Phone Numbers with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor