-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathReformatPhoneNumber.java
47 lines (41 loc) · 1.34 KB
/
ReformatPhoneNumber.java
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
44
45
46
47
package com.smlnskgmail.jaman.leetcodejava.easy;
import java.util.ArrayList;
import java.util.List;
// https://leetcode.com/problems/reformat-phone-number/
public class ReformatPhoneNumber {
private final String input;
public ReformatPhoneNumber(String input) {
this.input = input;
}
public String solution() {
if (input.length() < 4) {
return input;
}
List<Character> digits = new ArrayList<>();
for (int i = 0; i < input.length(); i++) {
char c = input.charAt(i);
if (c != '-' && c != ' ') {
digits.add(c);
}
}
StringBuilder result = new StringBuilder();
int diff = digits.size() % 3 == 0 ? 2 : (digits.size() - 2) % 3 == 0 ? 2 : 4;
for (int i = 0; i < digits.size() - diff; i += 3) {
if (i != 0) {
result.append('-');
}
for (int j = i; j < i + 3; j++) {
result.append(digits.get(j));
}
}
if (digits.size() % 3 != 0) {
for (int i = 0; i < diff; i++) {
if (result.length() != 0 && i % 2 == 0) {
result.append('-');
}
result.append(digits.get(digits.size() - diff + i));
}
}
return result.toString();
}
}