#1108

Defanging an IP Address

Easy StringBuilder

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

Replace every "." in an IPv4 address with "[.]".

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: rewrite the address with every "." turned into "[.]".

Why this works: StringBuilder lets you append piece by piece in O(n). Adding to a String with + inside a loop copies the whole string each time.

Example: "1.1.1.1"

'1' → "1"'.' → "1[.]"… → "1[.]1[.]1[.]1"

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Using replaceAll(".", ...) — replaceAll takes a regex, and "." matches every character.