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:
address is a valid IPv4 address.Problem Overview: You receive a valid IPv4 address as a string such as "1.1.1.1". The task is to "defang" it by replacing every period . with [.]. The result prevents the address from being interpreted as a clickable or executable IP. The challenge is purely string manipulation and focuses on safe and efficient character replacement.
Approach 1: String Replace Function (Time: O(n), Space: O(n))
The simplest solution uses the built‑in string replacement utility available in most languages. Call a method such as replace() to substitute every . with [.]. Internally, the runtime scans the string once and builds a new string with the replacements applied. Since each character is processed at most once, the time complexity is O(n) where n is the length of the address, and the new output string requires O(n) extra space.
This approach is concise and readable. It relies on standard library implementations that are already optimized. For problems centered on string manipulation, built‑in operations are often preferred because they reduce code complexity and avoid manual edge case handling.
Approach 2: Manual Character Replacement (Time: O(n), Space: O(n))
Instead of using a built‑in function, iterate through the string character by character. Maintain a result buffer (string builder, vector, or list depending on language). When the current character is ., append the sequence [.] to the result. Otherwise append the character itself. After processing all characters, convert the buffer into the final string.
This approach gives full control over how characters are appended and is useful when library replacements are unavailable or restricted. It demonstrates understanding of string processing and sequential traversal techniques. The loop processes each character once, so the runtime remains O(n). The result buffer stores the modified string, resulting in O(n) auxiliary space.
Recommended for interviews: The built‑in replace solution is usually the expected answer because the problem focuses on recognizing simple string transformation. Interviewers primarily want to see that you map the requirement directly to a clean operation. Implementing the manual iteration version still shows solid understanding of string traversal and how to construct output efficiently when library helpers are unavailable.
One straightforward approach to defang an IP address is to use a string replace function available in most programming languages. This involves replacing every period '.' with '[.]'. This method is efficient as most languages provide optimized functions for this purpose.
In this C solution, we iterate through the input string and construct the defanged IP address in the result string. Whenever a period '.' is encountered, we append '[.]' to the result. This is done using basic array manipulation and the strcat function for appending strings.
Time Complexity: O(n), where n is the length of the input string.
Space Complexity: O(n), as we are creating a new string for the output.
Another approach is to manually append each character to a new string or string builder, checking for periods and appending '[.]' instead of '.'. This provides more control over the process and can be more educational for understanding string processing.
This C code manually constructs the defanged IP address character by character. If the current character is a '.', we append '[.]' to the result array one character at a time. Otherwise, we simply append the current character.
Time Complexity: O(n), as we loop through each character once.
Space Complexity: O(n), for creating the output string buffer.
| Approach | Complexity |
|---|---|
| String Replace Function | Time Complexity: O(n), where n is the length of the input string. |
| Manual Character Replacement | Time Complexity: O(n), as we loop through each character once. |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| String Replace Function | O(n) | O(n) | Best choice in most languages when a built-in replace API is available. |
| Manual Character Replacement | O(n) | O(n) | Useful when library helpers are restricted or when demonstrating string traversal logic. |
LeetCode 1108: Defanging an IP Address - Interview Prep Ep 16 • Fisher Coder • 3,472 views views
Watch 9 more video solutions →Practice Defanging an IP Address with our built-in code editor and test cases.
Practice on FleetCode