Classic problem #1108EasyString
Defanging an IP Address
Real-world version: Wrapping the Dots of an AddressTracked 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)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 15 test cases: 15 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's