1108. Defanging an IP Address
easyReplace every period in an IPv4 address with '[.]' to neutralise it for safe logging. A single-pass scan with one substitution rule.
By Sam K., Founder, InterviewChamp.AI · Last verified
Problem
Given a valid (IPv4) IP address, return a defanged version of that IP address. A defanged IP address replaces every period '.' with '[.]'.
Constraints
The given address is a valid IPv4 address.address.length is between 7 and 15 inclusive.
Examples
Example 1
address = "1.1.1.1""1[.]1[.]1[.]1"Example 2
address = "255.100.50.0""255[.]100[.]50[.]0"Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
Hints
Progressive — try the first before opening the next.
Hint 1
A built-in replace works in one line: address.replace('.', '[.]').
Hint 2
For an interview, also show the manual build: walk each character, append '[.]' on a period and the character otherwise.
Hint 3
Use a list buffer and join at the end to avoid quadratic concatenation in languages where strings are immutable.
Solution approach
Reveal approach
Linear scan, appending '[.]' on each period and the character itself otherwise into a list, then join. O(n) time and O(n) space for the output. The built-in replace is equivalent in big-O terms but the manual version makes the cost model explicit — important when interviewers ask 'now write it without library calls'.
Complexity
- Time
- O(n)
- Space
- O(n)
Related patterns
- string-building
- linear-scan
Related problems
- 709. To Lower Case
- 1678. Goal Parser Interpretation
Asked at
Companies reported asking this problem (sourced from public Glassdoor, Blind, and Levels.fyi interview posts).
- Amazon
More Strings practice problems
- 3. Longest Substring Without Repeating Characters
- 5. Longest Palindromic Substring
- 6. Zigzag Conversion
- 14. Longest Common Prefix
- 20. Valid Parentheses
- 28. Find the Index of the First Occurrence in a String
- 49. Group Anagrams
- 58. Length of Last Word