Reformat Phone Number
number has digits, spaces and dashes. Remove the spaces and dashes, then group the digits from left to right into blocks of 3 separated by -, with this rule for the end:
- If 2 digits remain, they form a block of 2.
- If 4 digits remain, they form two blocks of 2.
- If 3 digits remain, they form a block of 3.
Return the formatted string.
Example 1
number "1-23-45 6"
returns "123-456"
Digits 123456 give 123-456.
Example 2
number "123 4-567"
returns "123-45-67"
Digits 1234567 give 123-45-67.
Constraints
2 <= len(number) <= 30- At least 2 digits appear
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.