-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReformatPhoneNumber.java
More file actions
44 lines (31 loc) · 1.17 KB
/
Copy pathReformatPhoneNumber.java
File metadata and controls
44 lines (31 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package LeetcodePractice;
public class ReformatPhoneNumber {
public static String reformatNumber(String number) {
String digits = number.replaceAll("[\\s-]", "");
StringBuilder result = new StringBuilder();
int i = 0;
int n = digits.length();
while (n - i > 4) {
result.append(digits.substring(i, i + 3)).append("-");
i += 3;
}
int remaining = n - i;
if (remaining == 4) {
result.append(digits.substring(i, i + 2)).append("-");
result.append(digits.substring(i + 2, i + 4));
} else if (remaining == 3) {
result.append(digits.substring(i, i + 3));
} else if (remaining == 2) {
result.append(digits.substring(i, i + 2));
}
return result.toString();
}
public static void main(String[] args) {
String number1 = "1-23-45 6";
System.out.println(reformatNumber(number1));
String number2 = "123 4-567";
System.out.println(reformatNumber(number2));
String number3 = "123 4-5678";
System.out.println(reformatNumber(number3));
}
}