-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path1108.java
More file actions
40 lines (40 loc) · 974 Bytes
/
Copy path1108.java
File metadata and controls
40 lines (40 loc) · 974 Bytes
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
// 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.
//
// Runtime 0 ms Beats 100.00% of users with Java
// Memory 40.62 MB Beats 33.40% of users with Java
class Solution {
public String defangIPaddr(String address) {
StringBuilder sb = new StringBuilder(address);
int index = 0;
while (index < sb.length()) {
if (sb.charAt(index) == '.') {
sb.insert(index, '[');
sb.insert(index + 2, ']');
index = index + 2;
} else {
index++;
}
}
return sb.toString();
}
}