Skip to main content

Defanging an IP Address - Solution & Explanation

EasyString11 min readAsked at: Amazon, Microsoft, Meta +4
Practice this problem

Problem Statement

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.

Approach Overview

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.

Approach 1: String Replace Function

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.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

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.

Try this approach in the editor →

Approach 2: Manual Character Replacement

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.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n), as we loop through each character once.
Space Complexity: O(n), for creating the output string buffer.

Try this approach in the editor →

Approach 3: Direct Replacement

We can directly replace the '.' in the string with '[.]'.

The time complexity is O(n), where n is the length of the string. Ignoring the space consumption of the answer, the space complexity is O(1).

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
String Replace Function

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.

Manual Character Replacement

Time Complexity: O(n), as we loop through each character once.
Space Complexity: O(n), for creating the output string buffer.

Direct Replacement—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
String Replace FunctionO(n)O(n)Best choice in most languages when a built-in replace API is available.
Manual Character ReplacementO(n)O(n)Useful when library helpers are restricted or when demonstrating string traversal logic.

Video Solution

LeetCode 1108: Defanging an IP Address - Interview Prep Ep 16 • Fisher Coder • 3,474 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Defanging an IP Address easy or hard?
Defanging an IP Address is categorized as an Easy problem. It focuses on basic string manipulation and recognizing when a built-in replace function solves the task in a single pass.
Defanging an IP Address Python/Java solution
In Python, use address.replace('.', '[.]'). In Java, use address.replace('.', '[.]') or replace(".", "[.]"). Both implementations scan the string once and return a new string with the defanged format.
How to solve Defanging an IP Address in O(n)?
Traverse the string once and replace every '.' with '[.]'. This can be done using a built-in replace method or by building a result string while iterating through characters. Since each character is processed once, the runtime remains O(n).
What is the best approach for Defanging an IP Address?
The most efficient and clean approach uses a built-in string replace function to substitute '.' with '[.]'. It processes the string once, giving O(n) time complexity and O(n) space for the new output string. This approach keeps the code short and leverages optimized standard library functions.
Is Defanging an IP Address asked at Google/Amazon/Meta?
Problems like Defanging an IP Address appear in interview preparation sets and practice platforms because they test basic string manipulation skills. While the exact question is more common in screening rounds or coding practice, similar string transformation tasks frequently appear in big tech interviews.
What data structure is used in Defanging an IP Address?
The primary data structure is a string. Some implementations also use a mutable buffer such as a StringBuilder, list, or character array to construct the result efficiently while iterating through the input.
What is the time complexity of Defanging an IP Address?
The time complexity is O(n), where n is the length of the IP address string. Each character is inspected once either by the replace function or by manual iteration. The algorithm performs a single pass through the string.

Ready to solve this problem?

Practice Defanging an IP Address with our built-in code editor and test cases.

Practice on FleetCode