Back to blog
Sep 07, 2024
4 min read

Defanging an IP Address

Given a valid IP address, replace every '.' with '[.]' to defang it.

Difficulty: Easy | Acceptance: 90.00% | Paid: No Topics: String

Given a valid IPv4 address address, return a defanged version of that IP address.

A defanged IP address replaces every period ”.” with ”[.]“.

Examples

Input: address = "1.1.1.1"
Output: "1[.]1[.]1[.]1"
Input: address = "255.100.50.0"
Output: "255[.]100[.]50[.]0"

Constraints

address contains only digits and '.'.
address is a valid IPv4 address.

Approach 1: Built-in String Replacement

Intuition Most modern programming languages provide built-in methods to replace all occurrences of a substring within a string. We can directly use these methods to replace every ”.” with ”[.]“.

Steps

  • Call the built-in replace function on the input string.
  • Pass ”.” as the target substring and ”[.]” as the replacement substring.
  • Return the result.
python
class Solution:
    def defangIPaddr(self, address: str) -> str:
        return address.replace('.', '[.]')

Complexity

  • Time: O(n) — The replace operation scans the string once.
  • Space: O(n) — A new string is created to store the result.
  • Notes: This is the most concise and readable solution in most languages.

Approach 2: Iterative Construction

Intuition We can iterate through the input string character by character. If we encounter a ’.’, we append ’[.]’ to our result; otherwise, we append the character itself.

Steps

  • Initialize an empty result string (or a mutable builder).
  • Loop through each character in the input string.
  • If the character is ’.’, append ’[.]’ to the result.
  • Else, append the character to the result.
  • Return the constructed string.
python
class Solution:
    def defangIPaddr(self, address: str) -> str:
        res = []
        for c in address:
            if c == '.':
                res.append('[.]')
            else:
                res.append(c)
        return ''.join(res)

Complexity

  • Time: O(n) — We iterate through the string once.
  • Space: O(n) — We store the result in a new string/buffer.
  • Notes: In languages like Java, using StringBuilder is more efficient than string concatenation in a loop.

Approach 3: Regular Expressions

Intuition We can use a regular expression to match all occurrences of the ’.’ character and replace them with the defanged string ’[.]‘. This is a powerful pattern-matching approach.

Steps

  • Define a regex pattern that matches the literal dot character.
  • Use the regex replace function to substitute all matches with ’[.]‘.
  • Return the modified string.
python
import re

class Solution:
    def defangIPaddr(self, address: str) -> str:
        return re.sub(r'\.', '[.]', address)

Complexity

  • Time: O(n) — The regex engine processes the string linearly.
  • Space: O(n) — Space for the new string and regex overhead.
  • Notes: While powerful, regex can be overkill for simple fixed-string replacements compared to built-in string methods.