09 Sept 2026Go / Python / TypeScriptEasy

Defanging an IP Address

Replace every period in an IPv4 address with a bracketed period.

Apply a global string replacement from each dot to the required defanged token.

complexity

O(n) time and O(n) output space.

solution files

  • Go defanging-an-ip-address/solution.go
  • Python defanging-an-ip-address/solution.py
  • TypeScript defanging-an-ip-address/solution.ts

Solution files

Godefanging-an-ip-address/solution.go
package main

import "strings"

func defangIPaddr(address string) string { return strings.ReplaceAll(address, ".", "[.]") }
Pythondefanging-an-ip-address/solution.py
class Solution:
    def defangIPaddr(self, address: str) -> str:
        return address.replace(class="syntax-string">".", class="syntax-string">"[.]")
TypeScriptdefanging-an-ip-address/solution.ts
function defangIPaddr(address: string): string {
  return address.replaceAll(class="syntax-string">".", class="syntax-string">"[.]");
}