Skip to content

Commit fd32110

Browse files
add 1694
1 parent 9e13784 commit fd32110

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|1694|[Reformat Phone Number](https://leetcode.com/problems/reformat-phone-number/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1694.java) ||Easy|String|
1112
|1690|[Stone Game VII](https://leetcode.com/problems/stone-game-vii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1690.java) ||Medium|DP|
1213
|1688|[Count of Matches in Tournament](https://leetcode.com/problems/count-of-matches-in-tournament/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1688.java) ||Easy|Backtracking|
1314
|1685|[Sum of Absolute Differences in a Sorted Array](https://leetcode.com/problems/sum-of-absolute-differences-in-a-sorted-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1685.java) ||Medium|Math, Greedy|
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.fishercoder.solutions;
2+
3+
public class _1694 {
4+
public static class Solution1 {
5+
public String reformatNumber(String number) {
6+
StringBuilder sb = new StringBuilder();
7+
for (char c : number.toCharArray()) {
8+
if (Character.isDigit(c)) {
9+
sb.append(c);
10+
}
11+
}
12+
String cleaned = sb.toString();
13+
sb.setLength(0);
14+
for (int i = 0; i < cleaned.length(); ) {
15+
if (i + 4 == cleaned.length()) {
16+
sb.append(cleaned.substring(i, i + 2));
17+
sb.append("-");
18+
sb.append(cleaned.substring(i + 2));
19+
break;
20+
} else if (i + 3 <= cleaned.length()) {
21+
sb.append(cleaned.substring(i, i + 3));
22+
sb.append("-");
23+
i += 3;
24+
} else {
25+
sb.append(cleaned.substring(i));
26+
break;
27+
}
28+
}
29+
if (sb.charAt(sb.length() - 1) == '-') {
30+
sb.setLength(sb.length() - 1);
31+
}
32+
return sb.toString();
33+
}
34+
}
35+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1694;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _1694Test {
10+
private static _1694.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _1694.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals("123-456", solution1.reformatNumber("1-23-45 6"));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals("123-45-67", solution1.reformatNumber("123 4-567"));
25+
}
26+
27+
@Test
28+
public void test3() {
29+
assertEquals("123-456-78", solution1.reformatNumber("123 4-5678"));
30+
}
31+
32+
@Test
33+
public void test4() {
34+
assertEquals("12", solution1.reformatNumber("12"));
35+
}
36+
37+
@Test
38+
public void test5() {
39+
assertEquals("175-229-353-94-75", solution1.reformatNumber("--17-5 229 35-39475 "));
40+
}
41+
42+
}

0 commit comments

Comments
 (0)