Defanging an IP Address

EASY

Description

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.

Approaches

Checkout 3 different approaches to solve Defanging an IP Address. Click on different approaches to view the approach and algorithm in detail.

Approach 2: Iteration with StringBuilder

This approach involves manually iterating through the input string character by character and building the result using a StringBuilder. A StringBuilder is chosen for its efficiency in string manipulation, as it avoids creating a new string object for every concatenation.

Algorithm

  • Initialize a new StringBuilder.
  • Iterate through each character of the input address string.
  • If the current character is a . (period), append the string "[.]" to the StringBuilder.
  • Otherwise, append the character itself.
  • After the loop completes, convert the StringBuilder to a string and return it.

This method provides direct control over the string construction process.

  1. We create an empty StringBuilder instance, let's call it sb.
  2. We loop through the input address string. For each character c:
  3. We check if c is equal to '.'.
  4. If it is, we append the replacement string "[.]" to sb.
  5. If it's not a period, we simply append the character c to sb.
  6. Once the loop has processed all characters in the address, the sb contains the defanged version. We call sb.toString() to get the final string and return it.
class Solution {
    public String defangIPaddr(String address) {
        StringBuilder sb = new StringBuilder();
        for (char c : address.toCharArray()) {
            if (c == '.') {
                sb.append("[.]");
            } else {
                sb.append(c);
            }
        }
        return sb.toString();
    }
}

Complexity Analysis

Time Complexity: O(N), where N is the length of the input string. We perform a single pass over the string, making the time complexity linear.Space Complexity: O(N), where N is the length of the input string. The `StringBuilder` will grow to the size of the output string, which is `N + 6` for a valid IPv4 address (since there are 3 periods, and each `.` of length 1 is replaced by `[.]` of length 3, a net increase of 2 characters per period).

Pros and Cons

Pros:
  • Highly efficient in terms of performance, as it involves a single pass through the string.
  • Avoids the overhead of regular expressions and creating intermediate data structures like arrays.
  • Memory usage is optimized by using a mutable StringBuilder.
Cons:
  • The code is more verbose than using a built-in replace method.
  • Requires manual implementation of the iteration and replacement logic.

Code Solutions

Checking out 3 solutions in different languages for Defanging an IP Address. Click on different languages to view the code.

class Solution {
public
  String defangIPaddr(String address) { return address.replace(".", "[.]"); }
}

Video Solution

Watch the video walkthrough for Defanging an IP Address



Data Structures:

String

Companies:

Subscribe to Scale Engineer newsletter

Learn about System Design, Software Engineering, and interview experiences every week.

No spam, unsubscribe at any time.