How I solved, Defanging an IP Address, on LeetCode
Hi Everyone,
Today I will be going over how I solved, Defanging an IP Address, on LeetCode.
Objective: Given a valid (IPv4) IP address
, return a defanged version of that IP address.
A defanged IP address replaces every period "."
with "[.]"
.
Level: Easy
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.
This challenge was very easy. I created an array called ipAddress that would store my new address. Then I created a for loop that will iterate through my address. If a period is encountered then it will push [.] into the array instead of the period. Any other character will push into the new array.
Once the for loop completes, it will join the array back into a string a return.
This solves the challenge.
Runtime: 72 ms, faster than 91.08% of JavaScript online submissions Defanging an IP Address.
Memory Usage: 38.7 MB, less than 12.29% of JavaScript online submissions for Defanging an IP Address.