Skip to contentKEEP THE CRAFTTHE SANCTUARY

Restore Valid IPv4 Addresses

Place three dots in a digit string to form every valid IPv4 address.

The problem

Implement Solution.restoreIpAddresses(text). text is a string of digits. Return every valid IPv4 address you can make by inserting three dots into text, without reordering, adding or removing digits. A valid address has exactly four parts, each a number from 0 to 255, and a part with more than one digit may not start with 0 ("0" is allowed, "01" is not). For example, "25525511135" gives "255.255.11.135" and "255.255.111.35". Return the addresses in any order, or an empty list if there are none. Constraints: text has 1 to 20 digits.

Starter code

class Solution:
    def restoreIpAddresses(self, text):
        pass

Public examples

  1. text = "25525511135"gives["255.255.11.135", "255.255.111.35"]
  2. text = "9999"gives["9.9.9.9"]
  3. text = "255255255255"gives["255.255.255.255"]

Solving it in the practice book needs an active paid plan.

Open it in the practice book
One thought at a time.