1108. Defanging an IP Address LeetCode Solution

In this guide, you will get 1108. Defanging an IP Address LeetCode Solution with the best time and space complexity. The solution to Defanging an IP Address problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Defanging an IP Address solution in C++
  4. Defanging an IP Address solution in Java
  5. Defanging an IP Address solution in Python
  6. Additional Resources
1108. Defanging an IP Address LeetCode Solution image

Problem Statement of Defanging an IP Address

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.

Complexity Analysis

  • Time Complexity:
  • Space Complexity:

1108. Defanging an IP Address LeetCode Solution in C++

class Solution {
 public:
  string defangIPaddr(string address) {
    return regex_replace(address, regex("[.]"), "[.]");
  }
};
/* code provided by PROGIEZ */

1108. Defanging an IP Address LeetCode Solution in Java

class Solution {
  public String defangIPaddr(String address) {
    return address.replace(".", "[.]");
  }
}
// code provided by PROGIEZ

1108. Defanging an IP Address LeetCode Solution in Python

class Solution:
  def defangIPaddr(self, address: str) -> str:
    return address.replace('.', '[.]')
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.