Restore IP Addresses - Problem
Restore IP Addresses is a classic string manipulation and backtracking problem that challenges you to reconstruct valid IP addresses from a string of digits.

Given a string s containing only digits, your task is to find all possible valid IP addresses that can be formed by inserting exactly three dots into the string. You cannot reorder or remove any digits - only insert dots at valid positions.

A valid IP address consists of exactly four integers separated by single dots, where each integer:
• Is between 0 and 255 (inclusive)
• Cannot have leading zeros (except for '0' itself)

Examples:
✅ Valid: "0.1.2.201", "192.168.1.1"
❌ Invalid: "0.011.255.245" (leading zero), "192.168.1.312" (> 255)

This problem tests your understanding of string validation, backtracking algorithms, and constraint satisfaction.

Input & Output

example_1.py — Basic Case
$ Input: "25525511135"
Output: ["255.255.11.135", "255.255.111.35"]
💡 Note: The string can be split into two valid IP addresses. Both combinations satisfy the constraints: no leading zeros and all segments ≤ 255.
example_2.py — Minimum Length
$ Input: "0000"
Output: ["0.0.0.0"]
💡 Note: Only one valid IP address can be formed. Each segment is "0" which is valid (no leading zero rule applies to single '0').
example_3.py — No Valid IPs
$ Input: "101023"
Output: ["1.0.10.23", "1.0.102.3", "10.1.0.23", "10.10.2.3", "101.0.2.3"]
💡 Note: Multiple valid combinations exist. Each segment is between 0-255 with no leading zeros.

Constraints

  • 1 ≤ s.length ≤ 20
  • s consists of digits only
  • Each IP segment must be between 0 and 255 (inclusive)
  • No leading zeros allowed (except for segment "0")

Visualization

Tap to expand
IP Address Reconstruction ProcessInput String: "25525511135"Goal: Insert 3 dots to create valid IP addressesStep 1: Try First Segment2. ? ? ?25. ? ?255. ? ?Step 2: Recursive Exploration255255111→ Valid IP→ Valid IPEarly Pruning Strategy• If remaining characters > segments_needed × 3 → TOO MANY (prune)• If remaining characters < segments_needed × 1 → TOO FEW (prune)• If segment > 255 or has leading zeros → INVALID (prune)This eliminates ~90% of impossible combinations early!Final Result: ["255.255.11.135", "255.255.111.35"]
Understanding the Visualization
1
Analyze the Problem
You have a string of digits and need to place exactly 3 dots to create 4 segments, each representing a valid IP octet (0-255, no leading zeros).
2
Start Backtracking
Begin at the first position and try creating segments of length 1, 2, or 3. For each valid segment, recursively solve for the remaining part.
3
Apply Early Pruning
Before exploring further, check if the remaining characters can possibly form the required number of segments. If not, backtrack immediately.
4
Validate and Collect
When exactly 4 segments are formed and the entire string is consumed, validate the complete IP address and add it to results.
Key Takeaway
🎯 Key Insight: Backtracking with early pruning transforms a potentially exponential search into a constant-time algorithm by eliminating invalid paths before exploring them fully. The constraint that IP addresses have exactly 4 segments, each with at most 3 digits, creates a naturally bounded search space.
Asked in
Google 42 Amazon 38 Meta 35 Microsoft 28 Apple 22
87.5K Views
Medium-High Frequency
~18 min Avg. Time
3.2K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen