Split a digit string into all valid IPv4 addresses by placing three dots.
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
0and255, 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
scontaining 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
scontains only digits.- Exactly four numeric segments must be formed.
- Each segment must represent an integer from
0to255. - Leading zeros are allowed only for the single digit
0. - All digits in
smust be used exactly once.
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.