Skip to content
Onsite

Restore IP Addresses

MediumP3Coding

s is a string of digits. Insert exactly three dots to turn it into a valid IPv4 address and return every valid result, in any order.

A valid address has four parts. Each part is a whole number from 0 to 255 written without a leading zero ("0" is fine, "01" is not). All digits of s must be used in order.

Example 1

Input
s = "25525511135"
Output
["255.255.11.135","255.255.111.35"]
Why
Two valid addresses.

Example 2

Input
s = "0000"
Output
["0.0.0.0"]
Why
Each part must be 0 because other splits would give a leading zero.

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

Constraints

  • 0 <= s.length <= 20
  • s contains only digits.

Hints

Run checks the 3 visible tests, Submit all 11.