Restore Valid IPv4 Addresses
Checking your account…
Sign in to save your code and progress across devices. The lesson and problem statement remain public.
Loading the interactive Interview workspace.If it does not appear, the problem and learning material remain readable, but browser execution is unavailable.Reload Interview workspace
Problem
Implement Solution.restoreIpAddresses(text). Return every address using all digits in four decimal segments from 0 to 255. Multi-digit segments may not start with zero. Output order does not matter.
Starter code
class Solution:
def restoreIpAddresses(self, text):
passTest cases
two-addresses
{
"args": [
"25525511135"
]
}Expected: ["255.255.11.135","255.255.111.35"]
Wizard outline
- Step 1: Initialize Solution.restoreIpAddresses
Replace the empty starter with the first real state owned by Solution.restoreIpAddresses. A small, named state is easier to verify than a complete algorithm. Establish it before adding the branch or loop that changes it.
- Step 2: Assemble the primary transition
Extend the initialized state with the next contiguous part of the popular solution. The transition explains how one input element or operation changes the state; boundaries are easier to reason about after this invariant is visible.
- Step 3: Pass the Two Addresses case
Complete the readable core algorithm for one representative Interview case. Build exactly four bounded segments and prune impossible remaining lengths.
- Step 4: Harden the Leading Zero boundary
Repair the reviewed boundary and pass the complete submission contract. Every valid address defines one sequence of four accepted segment lengths, which the search explores. Rejected segments violate an IPv4 rule, so emitted addresses are exactly the valid restorations.
Footguns and prerequisites
- The segment 0 is valid, but 00 and 01 are not.
- recursion and backtracking
Reviewed references
Practice prerequisites
- Complete One Backtracking Frame(opens in a new tab)
Complete One Backtracking Frame isolates before expanding each choice, path equals the original caller-owned prefix; every emitted candidate contains exactly one additional element. That focused state discipline is required when implementing restore ip addresses as a complete Interview Problem.
Recommended approach and implementation
Backtrack over segment lengths one through three, accepting non-leading-zero values at most 255 and emitting only after four segments consume all digits.
Why it works: Every valid address defines one sequence of four accepted segment lengths, which the search explores. Rejected segments violate an IPv4 rule, so emitted addresses are exactly the valid restorations.
class Solution:
def restoreIpAddresses(self, text):
"""
Checkpoint 1: initialize the state owned by this Interview contract.
Checkpoint 2: assemble the primary transition without hiding the boundary.
"""
result = []
segments = []
def backtrack(index):
if len(segments) == 4:
if index == len(text):
result.append('.'.join(segments))
return
remaining = len(text) - index
slots = 4 - len(segments)
if remaining < slots or remaining > 3 * slots:
return
for end in range(index + 1, min(index + 3, len(text)) + 1):
segment = text[index:end]
if len(segment) > 1 and segment[0] == '0':
break
if int(segment) > 255:
continue
segments.append(segment)
backtrack(end)
segments.pop()
backtrack(0)
return result