Given a valid IPv4 address, your task is to defang it. Defanging an IP address involves replacing 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:
Can you implement a function that takes an IPv4 address as a string and returns its defanged version? Describe the time and space complexity of your solution.
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force method for defanging an IP address means we carefully examine each character in the address one by one. When we find a period, we replace it with the special "[.]" sequence. We repeat this process until the entire address has been checked.
Here's how the algorithm would work step-by-step:
def defang_i_p_address(ip_address):
defanged_ip = ''
for character in ip_address:
# Need to replace periods with '[.]'
if character == '.':
defanged_ip += '[.]'
# All other characters are kept
else:
defanged_ip += character
return defanged_ip
The goal is to replace each period in the IP address with '[.]'. We can achieve this directly by going through the IP address and making the substitutions. This is efficient because we only look at each part of the IP address once.
Here's how the algorithm would work step-by-step:
def defang_ip_address(ip_address):
defanged_ip = ""
# Iterate through the IP address string.
for character in ip_address:
# Replace periods with '[.]'
if character == '.':
#Defanged IP requires replacement
defanged_ip += '[.]'
else:
defanged_ip += character
# Return the defanged IP address.
return defanged_ip
Case | How to Handle |
---|---|
Null or empty IP address string | Return an empty string or null, depending on requirements, after checking for null or empty input. |
IP address with invalid characters (e.g., letters, symbols other than dots) | Throw an IllegalArgumentException or return an error string indicating invalid input detected. |
IP address with more than three digits in a single octet | Throw an IllegalArgumentException or return an error string indicating invalid input, as each octet should not exceed 255. |
IP address with fewer or more than four octets | Throw an IllegalArgumentException or return an error string indicating invalid format (must have four octets separated by three dots). |
IP address with leading zeros in an octet (e.g., 192.168.001.1) | Consider this a valid octet by trimming the leading zeros, or reject based on specific requirements. |
Extremely long IP address string that could cause memory issues (DoS attack) | Check the input string length and reject excessively long inputs to prevent potential resource exhaustion. |
IP address with spaces surrounding the dots or numbers | Trim leading/trailing spaces from the entire IP string and from each octet before processing, or reject outright. |
IP address where an octet is greater than 255 | Throw an IllegalArgumentException or return an error string since octets must be between 0 and 255 inclusive. |