LeetCode Problem Workspace

Defanging an IP Address

Transform an IPv4 address by replacing each period with '[.]', focusing on a string-driven, direct manipulation approach in interviews.

category

1

Topics

code_blocks

5

Code langs

hub

3

Related

Practice Focus

Easy · String-driven solution strategy

bolt

Answer-first summary

Transform an IPv4 address by replacing each period with '[.]', focusing on a string-driven, direct manipulation approach in interviews.

Interview AiBox logo

Ace coding interviews with Interview AiBox guidance for String-driven solution strategy

Try AiBox Copilotarrow_forward

Start by iterating over the input string and appending characters to a result, substituting periods with '[.]'. This string-driven method is optimal for easy parsing and minimal logic errors. Using built-in string replace functions can simplify implementation, but manual iteration ensures clear control over character processing and avoids common off-by-one mistakes.

Problem Statement

Given a valid IPv4 address as a string, return a defanged version of this IP address. Each period '.' in the address must be replaced with '[.]' while keeping all other digits intact.

For example, if the input is '1.1.1.1', the output should be '1[.]1[.]1[.]1'. Implement this transformation efficiently, ensuring no extra characters are added and the final string correctly represents a defanged IP.

Examples

Example 1

Input: address = "1.1.1.1"

Output: "1[.]1[.]1[.]1"

Example details omitted.

Example 2

Input: address = "255.100.50.0"

Output: "255[.]100[.]50[.]0"

Example details omitted.

Constraints

  • The given address is a valid IPv4 address.

Solution Approach

Direct String Replacement

Use the built-in string replace method to substitute each '.' with '[.]'. This leverages language-level optimizations and reduces manual errors in character processing.

Iterative Concatenation

Iterate over each character in the input string. Append the character to a result buffer, and whenever a '.' is encountered, append '[.]' instead. This gives precise control and clarity over the string transformation.

Join After Split

Split the original string by '.', then join the resulting array with '[.]' as a separator. This approach avoids character-by-character iteration and is clean for short strings like IPv4 addresses.

Complexity Analysis

Metric Value
Time Depends on the final approach
Space Depends on the final approach

Time complexity is O(n) where n is the length of the input string since each character is processed once. Space complexity is also O(n) because a new string is created for the defanged IP.

What Interviewers Usually Probe

  • Checks understanding of string manipulation basics and built-in methods.
  • Wants clarity on iteration versus built-in replacements.
  • Looks for awareness of edge cases with input formatting.

Common Pitfalls or Variants

Common pitfalls

  • Forgetting to replace all periods, only handling the first occurrence.
  • Adding extra characters or incorrect brackets, like '[..]' instead of '[.]'.
  • Using regex incorrectly, causing unintended replacements or errors.

Follow-up variants

  • Defang IP addresses with alternative separators like '<.>' instead of '[.]'.
  • Handling mixed IPv4 and IPv6 addresses, applying defanging only to IPv4 parts.
  • Return defanged address while also validating correct IPv4 formatting before transformation.

FAQ

What does defanging an IP address mean?

It means replacing every period '.' in an IPv4 address with '[.]' to prevent accidental execution or parsing issues.

Which string-driven strategy is best for this problem?

Direct replacement using built-in functions is simplest, but iterative concatenation ensures precise control over each character.

Can I use regular expressions for defanging an IP address?

Yes, but simple replace or split/join methods are more readable and less error-prone for small IPv4 strings.

What is the time and space complexity of defanging an IP?

Both time and space complexity are O(n), where n is the length of the input string, since every character is processed and a new string is created.

Are there any edge cases to consider?

Yes, ensure the input is a valid IPv4 address and all periods are replaced exactly once without adding extra characters.

terminal

Solution

Solution 1: Direct Replacement

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

1
2
3
class Solution:
    def defangIPaddr(self, address: str) -> str:
        return address.replace('.', '[.]')
Defanging an IP Address Solution: String-driven solution strategy | LeetCode #1108 Easy