Skip to main content

Defanging an IP Address - Video Solutions

EasyString

LeetCode 1108: Defanging an IP Address - Interview Prep Ep 16

Fisher Coder
5:103,474 views
10 video solutions available

Defanging an IP Address - Video Solution

Watch 10 video solutions for Defanging an IP Address, a easy level problem involving String. This walkthrough by Fisher Coder has 3,474 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.

Problem Statement

Given a valid (IPv4) IP address, return a defanged version of that IP address.

A defanged IP address replaces every period "." with "[.]".

 

Example 1:

Input: address = "1.1.1.1"
Output: "1[.]1[.]1[.]1"

Example 2:

Input: address = "255.100.50.0"
Output: "255[.]100[.]50[.]0"

 

Constraints:

  • The given address is a valid IPv4 address.
Read full problem with examples

Approach Overview

Problem Overview: You receive a valid IPv4 address as a string such as "1.1.1.1". The task is to "defang" it by replacing every period . with [.]. The result prevents the address from being interpreted as a clickable or executable IP. The challenge is purely string manipulation and focuses on safe and efficient character replacement.

Approach 1: String Replace Function (Time: O(n), Space: O(n))

The simplest solution uses the built‑in string replacement utility available in most languages. Call a method such as replace() to substitute every . with [.]. Internally, the runtime scans the string once and builds a new string with the replacements applied. Since each character is processed at most once, the time complexity is O(n) where n is the length of the address, and the new output string requires O(n) extra space.

This approach is concise and readable. It relies on standard library implementations that are already optimized. For problems centered on string manipulation, built‑in operations are often preferred because they reduce code complexity and avoid manual edge case handling.

Approach 2: Manual Character Replacement (Time: O(n), Space: O(n))

Instead of using a built‑in function, iterate through the string character by character. Maintain a result buffer (string builder, vector, or list depending on language). When the current character is ., append the sequence [.] to the result. Otherwise append the character itself. After processing all characters, convert the buffer into the final string.

This approach gives full control over how characters are appended and is useful when library replacements are unavailable or restricted. It demonstrates understanding of string processing and sequential traversal techniques. The loop processes each character once, so the runtime remains O(n). The result buffer stores the modified string, resulting in O(n) auxiliary space.

Recommended for interviews: The built‑in replace solution is usually the expected answer because the problem focuses on recognizing simple string transformation. Interviewers primarily want to see that you map the requirement directly to a clean operation. Implementing the manual iteration version still shows solid understanding of string traversal and how to construct output efficiently when library helpers are unavailable.

Complexity Analysis

ApproachTimeSpaceWhen to Use
String Replace FunctionO(n)O(n)Best choice in most languages when a built-in replace API is available.
Manual Character ReplacementO(n)O(n)Useful when library helpers are restricted or when demonstrating string traversal logic.