forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_1108.java
34 lines (32 loc) · 951 Bytes
/
_1108.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
package com.fishercoder.solutions;
/**
* 1108. Defanging an IP Address
*
* Given a valid (IPv4) IP address, return a defanged version of that IP address.
* A defanged IP address replaces every period "." with "[.]".
*
* Example 1:
* Input: address = "1.1.1.1"
* Output: "1[.]1[.]1[.]1"
*
* Example 2:
* Input: address = "255.100.50.0"
* Output: "255[.]100[.]50[.]0"
*
* Constraints:
* The given address is a valid IPv4 address.
* */
public class _1108 {
public static class Solution1 {
public String defangIPaddr(String address) {
//String.replaceAll() takes in a regex which needs to be escaped
return address.replaceAll("\\.", "\\[\\.\\]");
}
}
public static class Solution2 {
public String defangIPaddr(String address) {
//String.replace() takes in a string which does NOT need to be escaped
return address.replace(".", "[.]");
}
}
}