Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit e2f7ac5

Browse files
add 1108
1 parent 21f75f6 commit e2f7ac5

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ _If you like this project, please leave me a star._ ★
2828
|1122|[Relative Sort Array](https://leetcode.com/problems/relative-sort-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1122.java) | O(n) | O(n) | |Easy||
2929
|1170|[Compare Strings by Frequency of the Smallest Character](https://leetcode.com/problems/compare-strings-by-frequency-of-the-smallest-character/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1170.java) | | | |Easy||
3030
|1119|[Remove Vowels from a String](https://leetcode.com/problems/remove-vowels-from-a-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1119.java) | | | [:tv:](https://www.youtube.com/watch?v=6KCBrIWEauw)|Easy||
31+
|1108|[Defanging an IP Address](https://leetcode.com/problems/defanging-an-ip-address/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1108.java) | | | |Easy||
3132
|1099|[Two Sum Less Than K](https://leetcode.com/problems/two-sum-less-than-k/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1099.java) | | | [:tv:](https://www.youtube.com/watch?v=2Uq7p7HE0TI)|Easy||
3233
|1089|[Duplicate Zeros](https://leetcode.com/problems/duplicate-zeros/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1089.java) | O(n^2) | O(1) | |Easy||
3334
|1086|[High Five](https://leetcode.com/problems/high-five/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1086.java) | | | [:tv:](https://www.youtube.com/watch?v=3iqC5J4l0Cc)|Easy||
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.fishercoder.solutions;
2+
3+
/**
4+
* 1108. Defanging an IP Address
5+
*
6+
* Given a valid (IPv4) IP address, return a defanged version of that IP address.
7+
* A defanged IP address replaces every period "." with "[.]".
8+
*
9+
* Example 1:
10+
* Input: address = "1.1.1.1"
11+
* Output: "1[.]1[.]1[.]1"
12+
*
13+
* Example 2:
14+
* Input: address = "255.100.50.0"
15+
* Output: "255[.]100[.]50[.]0"
16+
*
17+
* Constraints:
18+
* The given address is a valid IPv4 address.
19+
* */
20+
public class _1108 {
21+
public static class Solution1 {
22+
public String defangIPaddr(String address) {
23+
//String.replaceAll() takes in a regex which needs to be escaped
24+
return address.replaceAll("\\.", "\\[\\.\\]");
25+
}
26+
}
27+
28+
public static class Solution2 {
29+
public String defangIPaddr(String address) {
30+
//String.replace() takes in a string which does NOT need to be escaped
31+
return address.replace(".", "[.]");
32+
}
33+
}
34+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1108;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _1108Test {
10+
private static _1108.Solution1 solution1;
11+
private static _1108.Solution2 solution2;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _1108.Solution1();
16+
solution2 = new _1108.Solution2();
17+
}
18+
19+
@Test
20+
public void test1() {
21+
assertEquals("1[.]1[.]1[.]1", solution1.defangIPaddr("1.1.1.1"));
22+
assertEquals("1[.]1[.]1[.]1", solution2.defangIPaddr("1.1.1.1"));
23+
}
24+
25+
@Test
26+
public void test2() {
27+
assertEquals("255[.]100[.]50[.]0", solution1.defangIPaddr("255.100.50.0"));
28+
assertEquals("255[.]100[.]50[.]0", solution2.defangIPaddr("255.100.50.0"));
29+
}
30+
}

0 commit comments

Comments
 (0)