93. Restore IP Addresses
Asked at Oracle, NVIDIA
Difficulty
Medium
Time
O(1) in practice (at most 3^4 =
Space
O(1) extra besides the output (r
Pattern
String
Companies
2
Problem
You are given a string s that contains only digits. Your job is to put three dots into it so that it becomes a valid IP address, and return all such addresses.
A valid IP address has exactly four parts separated by single dots. Each part is a number from 0 to 255. A part may not have leading zeros, so "0" is fine but "01" and "011" are not. You must keep the digits in their original order — you can only insert dots, never move or delete digits.
Return the list of all valid addresses. The order of the answers does not matter. If no valid address exists, return an empty list.
Cut the digits into four parts
Constraints
- ●
1 <= s.length <= 20 - ●
s consists of digits only
Examples
s = "25525511135"["255.255.11.135","255.255.111.35"]s = "0000"["0.0.0.0"]s = "101023"["1.0.10.23","1.0.102.3","10.1.0.23","10.10.2.3","101.0.2.3"]Asked At
2 companies| Company | Difficulty | |
|---|---|---|
| Oracle | Medium | View all Oracle questions → |
| NVIDIA | Medium | View all NVIDIA questions → |
How to Think About It
Brute force: try every possible way to split s into 4 non-empty parts.
Use backtracking to build each octet one at a time.
Prune branches where the current octet exceeds 255 or has leading zeros.
At each step, try taking 1, 2, or 3 characters for the current octet.
When 4 octets are formed and all of s is consumed, add the result.
Optimal Approach
Use backtracking to try placing 1, 2, or 3 digits for each of the four octets. At each step, extract the substring, validate it (no leading zeros, value 0-255), and recurse for the next octet. When four valid octets are placed and no characters remain, add the IP to the result. The maximum recursion depth is 4 and each level branches at most 3 ways, giving O(1) effective time with string operations dominating at O(n).
Backtracking: pick 1, 2 or 3 digits per part
What Trips People Up in Real Interviews
Clarify edge cases: leading zeros (only "0" is valid), empty segments.
Mention pruning strategies to avoid unnecessary recursion.
Explain why at most 3 digits per octet bounds the recursion.
Discuss that the result set is small so backtracking is efficient.
Ask if duplicates need to be handled (input guarantees no ambiguity).
Solution Code
O(1) in practice (at most 3^4 = timedef restoreIpAddresses(s):
result = []
def backtrack(start, parts):
if len(parts) == 4:
if start == len(s):
result.append('.'.join(parts))
return
for length in range(1, 4):
if start + length > len(s):
break
segment = s[start:start + length]
if len(segment) > 1 and segment[0] == '0':
break
if int(segment) > 255:
break
backtrack(start + length, parts + [segment])
backtrack(0, [])
return resultFrequently Asked Questions
What is the Restore IP Addresses problem?
Given a string s containing only digits, return all possible valid IP addresses that can be formed by inserting dots into s. A valid IP address consists of exactly four integers separated by single dots, where each integer is between 0 and 255.
How do you solve Restore IP Addresses?
The optimal approach runs in O(1) in practice (at most 3^4 = time and O(1) extra besides the output (r space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.
What companies ask Restore IP Addresses?
Restore IP Addresses is asked at Oracle, NVIDIA. It is a medium difficulty problem.
What are common mistakes on Restore IP Addresses?
- Clarify edge cases: leading zeros (only "0" is valid), empty segments.
- Mention pruning strategies to avoid unnecessary recursion.
- Explain why at most 3 digits per octet bounds the recursion.
- Discuss that the result set is small so backtracking is efficient.
- Ask if duplicates need to be handled (input guarantees no ambiguity).