Given a string number
representing a phone number, reformat it so that the digits are grouped and separated by dashes '-'
according to the following rules:
Constraints:
'-'
.The problem asks us to reformat a phone number string by grouping digits and adding dashes according to specific rules. At first, the brute-force approach would be to process the input character by character, grouping them into sets of three, and handling the special cases at the end. However, upon closer inspection, we realize that:
By focusing on removing non-digit characters first, and then grouping the digits, we can simplify the implementation and make the solution more readable and efficient.
Let's break down the steps to solve the problem:
This approach ensures that we only process the string a couple of times (once to clean and once to group), and we handle the special cases for the last few digits as required by the problem statement.
Let's walk through an example with the input: number = "1-23-45 6"
123456
123
456
123-456
Another example: number = "123 4-567"
1234567
123
456
7
, but since the last group cannot be a single digit, we should have stopped before and split the last 4 digits into two groups of 2: 12-34-56-7
is invalid. Instead, we do: 123-45-67
class Solution:
def reformatNumber(self, number: str) -> str:
digits = [c for c in number if c.isdigit()]
res = []
i = 0
n = len(digits)
while n - i > 4:
res.append(''.join(digits[i:i+3]))
i += 3
if n - i == 4:
res.append(''.join(digits[i:i+2]))
res.append(''.join(digits[i+2:i+4]))
else:
res.append(''.join(digits[i:]))
return '-'.join(res)
class Solution {
public:
string reformatNumber(string number) {
string digits;
for (char c : number)
if (isdigit(c))
digits += c;
vector<string> res;
int i = 0, n = digits.size();
while (n - i > 4) {
res.push_back(digits.substr(i, 3));
i += 3;
}
if (n - i == 4) {
res.push_back(digits.substr(i, 2));
res.push_back(digits.substr(i + 2, 2));
} else {
res.push_back(digits.substr(i));
}
string ans = res[0];
for (int j = 1; j < res.size(); ++j)
ans += "-" + res[j];
return ans;
}
};
class Solution {
public String reformatNumber(String number) {
StringBuilder digits = new StringBuilder();
for (char c : number.toCharArray()) {
if (Character.isDigit(c)) {
digits.append(c);
}
}
List<String> res = new ArrayList<>();
int i = 0, n = digits.length();
while (n - i > 4) {
res.add(digits.substring(i, i + 3));
i += 3;
}
if (n - i == 4) {
res.add(digits.substring(i, i + 2));
res.add(digits.substring(i + 2, i + 4));
} else {
res.add(digits.substring(i, n));
}
return String.join("-", res);
}
}
var reformatNumber = function(number) {
let digits = [];
for (let c of number) {
if (c >= '0' && c <= '9') digits.push(c);
}
let res = [];
let i = 0, n = digits.length;
while (n - i > 4) {
res.push(digits.slice(i, i + 3).join(''));
i += 3;
}
if (n - i === 4) {
res.push(digits.slice(i, i + 2).join(''));
res.push(digits.slice(i + 2, i + 4).join(''));
} else {
res.push(digits.slice(i).join(''));
}
return res.join('-');
};
Brute-force approach: If we tried to generate all possible groupings and check which one matches the rules, the time complexity would be much higher and unnecessary.
Optimized approach (as above):
n
is the length of the input string. We traverse the string once to filter digits, and once to group them.To solve the Reformat Phone Number problem, we first remove all non-digit characters, then group the digits according to the rules (mainly handling the special case for 4 digits at the end), and finally join the groups with dashes. The solution is straightforward, efficient, and only requires a couple of passes through the data. The key insight is to handle the grouping from left to right and pay special attention to the last few digits. This approach is both readable and optimal for the given constraints.