LeetCode 1108: Defanging an IP Address Solution
Master LeetCode problem 1108 (Defanging an IP Address), a easy challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.
1108. Defanging an IP Address
Problem Explanation
Explanation:
To defang an IP address, we need to replace every period "." with "[.]". We can achieve this by iterating through the characters of the IP address string and constructing a new string with the required replacements.
- Time complexity: O(n), where n is the length of the input IP address string.
- Space complexity: O(n), where n is the length of the input IP address string.
:
Solution Code
class Solution {
public String defangIPaddr(String address) {
StringBuilder defanged = new StringBuilder();
for (char c : address.toCharArray()) {
if (c == '.') {
defanged.append("[.]");
} else {
defanged.append(c);
}
}
return defanged.toString();
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 1108 (Defanging an IP Address)?
This page provides optimized solutions for LeetCode problem 1108 (Defanging an IP Address) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.
What is the time complexity of LeetCode 1108 (Defanging an IP Address)?
The time complexity for LeetCode 1108 (Defanging an IP Address) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 1108 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1108 in Java, C++, or Python.