InferenceAI

Enter at least two characters to search.

Start free

Restore the Address

mediumRecursion & Backtracking25 min25 XP

Return every valid IPv4 address that can be formed by inserting three dots into the digit string s, in lexicographic order. Each of the four parts must be between 0 and 255 with no leading zeros ("0" is fine, "01" is not).

Examples

EXAMPLE WALKTHROUGHtwo answers

A guided view of the published example, not a trace of your code.

INPUT["25525511135"]
TASKrestore_address()
EXPECTED OUTPUT["255.255.11.135","255.255.111.35"]

Start with the input values shown in this example.

Example 1 of 3

Input: s = "25525511135"

Output: ["255.255.11.135","255.255.111.35"]

Explanation: two answers

Input: s = "0000"

Output: ["0.0.0.0"]

Explanation: zeros

Input: s = "101023"

Output: ["1.0.10.23","1.0.102.3","10.1.0.23","10.10.2.3","101.0.2.3"]

Explanation: several

Hint 1

Each part has 1 to 3 digits; backtrack over the four parts.

Hint 2

Prune when the remaining digits can't fill the remaining parts.

Topics

DSARecursion & BacktrackingPython