AlgoMaster Logo

Restore IP Addresses

mediumFrequencyUpdated September 21, 2026

Understanding the Problem

We're given a string of digits and need to find every way to split it into exactly four parts, where each part is a valid IP address segment: an integer from 0 to 255 with no leading zeros.

A segment can be 1, 2, or 3 digits long, and two rules decide validity. A segment like "01" is invalid because of the leading zero, while "0" by itself is fine. A segment like "256" is invalid because it exceeds 255.

This is a partitioning problem with constraints. We place three dots in the string to divide it into four segments, and every segment must pass the validity check. Since each segment is at most 3 digits and there are always exactly 4 segments, the search space is small. The three dots have at most 3 x 3 x 3 = 27 possible placements.

Key Constraints:

  • 1 <= s.length <= 20. A valid IP needs at least 4 characters (four single-digit segments) and at most 12 (four 3-digit segments). Any input outside that range produces an empty result, which lets us reject extreme inputs immediately. Within the range, the 4-segment structure caps the work at 27 combinations.
  • s consists of digits only. There are no non-digit characters to reject, so validity reduces to the leading-zero and 0-255 checks.

Approach 1: Brute Force (Three Nested Loops)

Intuition

We place 3 dots in the string, which creates 4 segments. Three nested loops cover every placement, one loop per dot. The first loop chooses where the first dot goes (after 1, 2, or 3 characters), the second loop chooses the second dot, and the third chooses the third dot. Whatever remains after the third dot becomes the fourth segment.

For each combination, we check whether all four segments are valid. If they are, we record the IP address.

This directly models the problem with no recursion and no extra data structures: try every way to split the string into four parts and keep the valid ones.

Algorithm

  1. Loop i from 1 to 3 (length of the first segment).
  2. For each i, loop j from i+1 to i+3 (end position of the second segment).
  3. For each j, loop k from j+1 to j+3 (end position of the third segment).
  4. The fourth segment runs from k to the end of the string.
  5. Extract all four substrings and check if each is a valid IP segment (no leading zeros, value between 0 and 255).
  6. If all four segments are valid, combine them with dots and add to the result.

Example Walkthrough

1Initial string: try placing 3 dots to form 4 segments
0
2
1
5
2
5
3
2
4
5
5
5
6
1
7
1
8
1
9
3
10
5
1/5

Code

The three nested loops are hardcoded for exactly 4 segments and validate each split only after building all four substrings. The next approach expresses the same search recursively, which lets it abandon a branch the moment a partial segment is invalid instead of always running the inner loops to completion.

Approach 2: Backtracking

Instead of three nested loops, we express the search recursively. At each step we choose how many characters to take for the current segment: 1, 2, or 3. If that segment is valid, we recurse to fill the remaining segments from the rest of the string. When we have 4 segments and have consumed the entire string, we record the IP address.

This is backtracking over a decision tree where each node chooses 1, 2, or 3 characters for one segment. We prune a branch as soon as a segment is invalid. If the first segment "00" has a leading zero, every combination starting with it is dropped at once rather than rebuilt and rechecked in inner loops. The brute force, by contrast, only discovers that the split fails after constructing all four substrings.

Algorithm

  1. Start with an empty list of segments and position 0 in the string.
  2. At each recursive call, try taking 1, 2, or 3 characters starting from the current position.
  3. For each choice, validate the segment (no leading zeros, value 0-255).
  4. If valid, add the segment to the current path and recurse for the next segment.
  5. If we have exactly 4 segments and have used all characters, add the IP address to results.
  6. Backtrack by removing the last segment before trying the next length.

Example Walkthrough

1Start: backtrack(start=0, segments=[])
0
1
start
1
0
2
1
3
0
4
2
5
3
1/8

Code

Both approaches run in constant time because the IP structure fixes the search at no more than 27 dot placements regardless of input length. The brute force is the most direct to write and reason about. The backtracking version generalizes more cleanly (changing 4 to any segment count is a one-line edit) and prunes invalid prefixes before building the rest of the split, so it touches fewer combinations on inputs with many early rejections. For this problem either is a fine answer; backtracking is the more reusable template for the broader family of string-partitioning problems.