Defanging an IP Address - LeetCode

Given a valid (IPv4) IP address, return a defanged version of that IP address.

defanged IP address replaces every period "." with "[.]".

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/0fb9c488-f7a0-4c43-b4d7-0fceeb0e89cd/Screen_Shot_2021-03-29_at_3.43.36_PM.png

  1. type conversion input string to list

  2. if compiler finds '.', replace with '[.]'

  3. type conversion back to string

"""
1108. 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 "[.]".

Input: address = "1.1.1.1"
Output: "1[.]1[.]1[.]1"
"""

address = list("1.1.1.1")
print(address)

i = 0
while i < len(address):
    if address[i] == ".":
        address.insert(i, "[.]")
        address.remove(".")

    i += 1

print(''.join(address))

in leetcode

in leetcode

How to convert list to string

''.join(list)
' '.join(list)