Bird
Raised Fist0
Interview PrepbacktrackingmediumAmazonFacebookGoogle

Restore IP Addresses

Choose your preparation mode3 modes available
</>
IDE
def restore_ip_addresses(s: str) -> list:public List<String> restoreIpAddresses(String s)vector<string> restoreIpAddresses(string s)function restoreIpAddresses(s)
def restore_ip_addresses(s: str) -> list:
    # Write your solution here
    pass
class Solution {
    public List<String> restoreIpAddresses(String s) {
        // Write your solution here
        return new ArrayList<>();
    }
}
#include <vector>
#include <string>
using namespace std;

vector<string> restoreIpAddresses(string s) {
    // Write your solution here
    return {};
}
function restoreIpAddresses(s) {
    // Write your solution here
}
0/10
Common Bugs to Avoid
Wrong: ["255.255.111.35", "255.255.11.135", "255.255.111.135"]Including segments longer than 3 digits or not pruning segments > 255.Add condition to skip segments longer than 3 or with integer value > 255.
Wrong: ["0.0.0.0", "00.0.0.0"]Allowing segments with leading zeros like '00'.Reject segments starting with '0' unless segment is exactly '0'.
Wrong: ["1.0.10.23", "1.0.102.3", "10.1.0.23", "10.10.2.3"]Missing some valid IPs due to incomplete backtracking or pruning too early.Ensure backtracking explores all segment length options 1 to 3 and validates each segment.
Wrong: ["255.255.255.256"]Not checking upper bound of 255 for segments.Add check to reject segments with integer value > 255.
Wrong: ["0.10.0.10", "0.100.1.0", "0.01.0.10"]Allowing segments with leading zeros like '01'.Reject segments starting with '0' and length > 1.
Test Cases
t1_01basic
Input{"s":"25525511135"}
Expected["255.255.11.135","255.255.111.35"]

The string can be split into these two valid IP addresses by placing dots at appropriate positions.

t1_02basic
Input{"s":"101023"}
Expected["1.0.10.23","1.0.102.3","10.1.0.23","10.10.2.3","101.0.2.3"]

Multiple valid IP addresses can be formed by splitting the string into 4 valid segments.

t2_01edge
Input{"s":""}
Expected[]

Empty input string cannot form any valid IP address.

t2_02edge
Input{"s":"1"}
Expected[]

Input length less than 4 cannot form a valid IP address with 4 segments.

t2_03edge
Input{"s":"0000"}
Expected["0.0.0.0"]

All segments are '0', which is valid; no leading zeros issue here.

t2_04edge
Input{"s":"1234567890123"}
Expected[]

Input length greater than 12 cannot form valid IP addresses since max 3 digits per segment and 4 segments.

t3_01corner
Input{"s":"255255255255"}
Expected["255.255.255.255"]

Maximum valid IP address with all segments at upper boundary 255.

t3_02corner
Input{"s":"010010"}
Expected["0.10.0.10","0.100.1.0"]

Tests handling of leading zeros and multiple valid splits.

t3_03corner
Input{"s":"1111"}
Expected["1.1.1.1"]

Minimal valid IP with all segments length 1.

t4_01performance
Input{"s":"12345678901234567890"}
⏱ Performance - must finish in 2000ms

Input length 20 (max constraint). Backtracking with pruning must complete within 2 seconds.