How I solved, Defanging an IP Address, on LeetCode

Danielle Torres
1 min readJun 13, 2021

--

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.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Danielle Torres
Danielle Torres

Written by Danielle Torres

Highly organized & motivated Software Engineer with an application analyst/data analyst background.

No responses yet

Write a response