Domain 1EasyCORE

Reformat Phone Number

The problem requires reformatting a given phone number string by removing spaces and dashes, then grouping the remaining digits into blocks of length 3 until there are 4 or fewer digits. The final digits are grouped as follows: - 2 digits: A single block of length 2. - 3 digits: A single block of length 3. - 4 digits: Two blocks of length 2 each.

Domain: Easy•Updated: 2026-08-31
#Algorithms#LeetCode#qwen2.5-coder:14b#String

1694. Reformat Phone Number

Model: qwen2.5-coder:14b
Topics: String
FAANG asked: 0
Acceptance rate: 65.4%


Problem Breakdown

The problem requires reformatting a given phone number string by removing spaces and dashes, then grouping the remaining digits into blocks of length 3 until there are 4 or fewer digits. The final digits are grouped as follows:

  • 2 digits: A single block of length 2.
  • 3 digits: A single block of length 3.
  • 4 digits: Two blocks of length 2 each.

The blocks are then joined by dashes. The constraints ensure that the input string has at least two digits and a maximum length of 100 characters.

Intuition

A brute-force approach would involve iterating through the string, removing non-digit characters, and grouping digits into blocks of 3 until there are 4 or fewer digits left. However, this approach could be inefficient in terms of time complexity due to repeated operations on the string.

The key insight is to first remove all spaces and dashes from the input string. Then, process the remaining digits by repeatedly taking chunks of 3 digits until there are 4 or fewer digits left. For the final chunk, handle the cases where there are 2, 3, or 4 digits appropriately.

Approach

  1. Remove all non-digit characters from the input string.
  2. Initialize an empty list to store the formatted blocks.
  3. While the length of the remaining digits is greater than 4:
    • Append a block of the first 3 digits to the list.
    • Remove these 3 digits from the beginning of the string.
  4. Handle the final chunk of digits based on its length:
    • If there are 2 or 3 digits, append them as a single block.
    • If there are 4 digits, split them into two blocks of 2 digits each.
  5. Join all blocks with dashes and return the result.

Complexity Analysis

  • Time Complexity: O(n), where n is the length of the input string. This is because we iterate through the string once to remove non-digit characters and then process the remaining digits in linear time.
  • Space Complexity: O(n), as we store the formatted blocks in a list, which can be at most half the length of the input string (if all characters are digits).

Solution

def reformatNumber(number: str) -> str:
    # Remove all non-digit characters
    digits = [char for char in number if char.isdigit()]
    
    formatted_blocks = []
    
    while len(digits) > 4:
        # Append a block of the first 3 digits
        formatted_blocks.append(''.join(digits[:3]))
        # Remove these 3 digits from the beginning
        digits = digits[3:]
    
    # Handle the final chunk of digits
    if len(digits) == 2 or len(digits) == 3:
        formatted_blocks.append(''.join(digits))
    elif len(digits) == 4:
        formatted_blocks.append(''.join(digits[:2]))
        formatted_blocks.append(''.join(digits[2:]))
    
    # Join all blocks with dashes
    return '-'.join(formatted_blocks)

Edge Cases Handled

  • Input with only digits: The code handles inputs like “1234567890” by grouping them into blocks of 3.
  • Input with exactly 4 digits left: The code splits these into two blocks of 2 digits each, as in “1234”.
  • Input with exactly 2 or 3 digits left: The code handles these cases by appending them as a single block.
  • Input with spaces and dashes: The code removes all non-digit characters before processing the digits.