-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDefangIPaddr.java
More file actions
32 lines (27 loc) · 829 Bytes
/
DefangIPaddr.java
File metadata and controls
32 lines (27 loc) · 829 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
/***
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"
Runtime: 0 ms, faster than 100.00% of Java online submissions for Defanging an IP Address.
Memory Usage: 34.6 MB, less than 100.00% of Java online submissions for Defanging an IP
***/
class Solution {
public String defangIPaddr(String address) {
String output = "";
for(int i = 0; i < address.length(); i++) {
char c = address.charAt(i);
if(c == '.') {
output += "[.]";
}
else {
output += c;
}
}
return output;
}
}