Show problems as
Classic problem #1108EasyString

Defanging an IP Address

Real-world version: Wrapping the Dots of an Address
Tracked in this browser only
Read Defanging an IP Address on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

The string address is a valid IPv4 address: four groups of decimal digits joined by single dots, as in 192.168.0.1.

Return the address with every dot swapped for the three characters [.].

Examples

Example 1

Input
address = "192.168.0.1"
Output
"192[.]168[.]0[.]1"

Each of the three dots becomes the bracketed form and the digit groups pass through untouched.

Example 2

Input
address = "0.0.0.0"
Output
"0[.]0[.]0[.]0"

All four groups are a single zero, and the three dots are wrapped just the same.

Example 3

Input
address = "12.34.56.78"
Output
"12[.]34[.]56[.]78"

Only the dots change, so all eight digits come through exactly as they were.

Constraints

  • address is a valid IPv4 address

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def defang_i_paddr(address: str) -> str:
Java
public String defangIPaddr(String address)