Practice Reformat Phone Number
Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.
Problem Statement
Remove spaces and hyphens from number. From left to right, take groups of three digits while more than four remain. Finish with one group when two or three digits remain, or two groups of two when four remain. Join all groups with hyphens and return the formatted number.
Source: Reformat Phone Number on LeetCode. Use the original task for its coding environment and submissions.
Examples
# Example 1
Input
number = "1-23-45 6"
Output
"123-456"
# Example 2
Input
number = "123 4-567"
Output
"123-45-67"
# Example 3
Input
number = "123 4-5678"
Output
"123-456-78"
Constraints
2 <= number.length <= 100
number consists of digits and the characters '-' and ' '.
There are at least two digits in number.
Related LeetCode Problems
Practice more problems similar to Reformat Phone Number.
Review your practice with PhantomCodeAI
After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.