Skip to main content
Back to problems
Leetcode
Medium
Strings
Backtracking
Recursion
Amazon
Microsoft
Restore IP Addresses

Split a digit string into all valid IPv4 addresses by placing three dots.

Acceptance 0%
Problem Statement

Problem

Given a string consisting only of digits, return all possible valid IPv4 addresses that can be formed by inserting exactly three dots into the string.

A valid IPv4 address has four parts separated by dots. Each part must:

  • be between 0 and 255, inclusive
  • not contain leading zeros unless the part is exactly 0

You must use all digits in the original string and keep their relative order.

Return every valid address that can be formed.

Input Format

  • A single string s containing only digits.
  • The string length is small enough to allow exhaustive partitioning.

Output Format

  • Return a list of all valid IPv4 addresses that can be formed from s.
  • The order of answers does not matter.

Constraints

  • s contains only digits.
  • Exactly four numeric segments must be formed.
  • Each segment must represent an integer from 0 to 255.
  • Leading zeros are allowed only for the single digit 0.
  • All digits in s must be used exactly once.
Examples
Sample cases returned by the problem API.

Example 1

Input

s = "25525511135"

Output

["255.255.11.135", "255.255.111.35"]

Explanation

Two valid ways to place three dots while keeping each segment in range.

Example 2

Input

s = "0000"

Output

["0.0.0.0"]

Explanation

Each segment can be exactly 0, but multi-digit segments with leading zeros are invalid.

Show 1 more example

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"]

Explanation

All digits must be used, and every segment must satisfy the IPv4 rules.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.