The Leetcode problem "Defanging an IP Address" asks you to transform a given valid IPv4 address into a "defanged" version. An IPv4 address is a string in the format "X.X.X.X"
where X
is a number between 0 and 255, and periods '.'
separate the numbers.
To defang an IP address, you must replace every period '.'
with "[.]"
. The rest of the characters remain unchanged.
Constraints:
address
is a valid IPv4 address."1.1.1.1"
"1[.]1[.]1[.]1"
When approaching this problem, the first idea that comes to mind is to look for every period '.'
in the string and replace it with "[.]"
. Since the input is guaranteed to be a valid IPv4 address, we do not need to validate the format or handle edge cases with invalid input.
A brute-force approach would be to iterate through each character in the string, check if it is a period, and build a new string accordingly. Alternatively, many programming languages provide string replacement functions that can directly substitute all occurrences of a character with a new substring, making the solution concise and efficient.
The main focus is to ensure that every period is replaced, and no other characters are altered. Optimization is straightforward here, as the problem is simple and the input size is small (IPv4 addresses are short strings).
To solve the problem, we can use either a manual traversal or a built-in string replacement function. Here’s how both can be approached:
replace
in Python, Java, and JavaScript, or std::replace
in C++) that can replace all instances of a character or substring with another.'.'
with "[.]"
."[.]"
to the result string.In practice, the string replacement method is preferred due to its simplicity and directness.
Let’s walk through an example with the input "192.168.0.1"
:
"192[.]168[.]0[.]1"
.
If using a built-in replace
function, the entire process is reduced to a single operation: address.replace('.', '[.]')
.
class Solution:
def defangIPaddr(self, address: str) -> str:
return address.replace('.', '[.]')
class Solution {
public:
string defangIPaddr(string address) {
string result;
for (char c : address) {
if (c == '.') {
result += "[.]";
} else {
result += c;
}
}
return result;
}
};
class Solution {
public String defangIPaddr(String address) {
return address.replace(".", "[.]");
}
}
/**
* @param {string} address
* @return {string}
*/
var defangIPaddr = function(address) {
return address.replace(/\./g, '[.]');
};
Brute-force approach (manual traversal):
In both approaches, the complexity is linear in the size of the input string, which is very efficient given the typically short length of IPv4 addresses.
The "Defanging an IP Address" problem is a straightforward string manipulation exercise. The key is to replace every period '.'
with "[.]"
in the given IPv4 address. Built-in string replacement functions make the implementation concise and efficient, while a manual character-by-character approach reinforces fundamental programming concepts. The problem’s simplicity and clear constraints enable a direct and elegant solution with linear time and space complexity.