Watch 10 video solutions for Restore IP Addresses, a medium level problem involving String, Backtracking. This walkthrough by NeetCode has 59,514 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
A valid IP address consists of exactly four integers separated by single dots. Each integer is between 0 and 255 (inclusive) and cannot have leading zeros.
"0.1.2.201" and "192.168.1.1" are valid IP addresses, but "0.011.255.245", "192.168.1.312" and "192.168@1.1" are invalid IP addresses.Given a string s containing only digits, return all possible valid IP addresses that can be formed by inserting dots into s. You are not allowed to reorder or remove any digits in s. You may return the valid IP addresses in any order.
Example 1:
Input: s = "25525511135" Output: ["255.255.11.135","255.255.111.35"]
Example 2:
Input: s = "0000" Output: ["0.0.0.0"]
Example 3:
Input: s = "101023" Output: ["1.0.10.23","1.0.102.3","10.1.0.23","10.10.2.3","101.0.2.3"]
Constraints:
1 <= s.length <= 20s consists of digits only.Problem Overview: Given a string containing only digits, generate all possible valid IPv4 addresses by inserting three dots. Each address must contain exactly four segments, and each segment must be between 0 and 255 without leading zeros (except the number 0 itself).
Approach 1: Backtracking (O(n^3) time, O(1) extra space)
This approach builds the IP address segment by segment using backtracking. Start from the first character and try to take 1, 2, or 3 digits as the next segment. After selecting a segment, validate it: the integer must be ≤255 and must not contain leading zeros like "01". If the segment is valid, recursively continue building the next part until four segments are formed.
The recursion stops when either four segments are created or the string is fully consumed. If both conditions match simultaneously, join the segments using dots and add the result to the output. Because each segment can only be length 1–3 and there are exactly four segments, the search space is small. This method systematically explores combinations while pruning invalid branches early.
This solution heavily relies on string slicing and validation checks. Interviewers expect candidates to reason about constraints such as segment length, leading zeros, and numeric bounds. The recursion depth is at most four, so auxiliary space remains constant aside from the output list.
Approach 2: Iterative Split Enumeration (O(n^3) time, O(1) extra space)
An alternative approach avoids recursion and instead enumerates all possible ways to place three dots using nested loops. Use three indices i, j, and k to represent split points in the string. These indices define four segments: s[0:i], s[i:j], s[j:k], and s[k:n]. Each segment must be length 1–3, so the loops only explore valid boundaries.
For every split combination, validate the four segments individually. Convert each substring to an integer and check the constraints: value ≤255 and no leading zeros unless the segment is exactly "0". If all segments pass validation, combine them into a dotted string and add it to the result list.
This method performs explicit enumeration instead of recursion, which some developers find easier to reason about during debugging. It still relies on careful string manipulation and boundary checks.
Recommended for interviews: The backtracking solution is the expected answer. It demonstrates control over recursive state exploration and pruning invalid candidates early. The iterative split method also works and shows strong reasoning about segment boundaries, but backtracking better reflects the pattern interviewers associate with constrained combinatorial generation problems.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Backtracking | O(n^3) | O(1) auxiliary (depth ≤ 4) | Best general solution. Cleanly explores segment combinations with early pruning. |
| Iterative Split Enumeration | O(n^3) | O(1) | When recursion is avoided and you want explicit control over split indices. |