Skip to content

Restore IP Addresses

MediumBacktracking
Decode it first. Clues, constraints, pattern and plan before any code.

A valid IPv4 address is exactly four numbers separated by dots, where each number is between 0 and 255 and has no leading zeros (0 is fine, 01 is not).

Given a string s of digits, insert three dots to form valid addresses. You may not reorder or drop digits. Return every valid address, in any order.

"25525511135"  →  ["255.255.11.135", "255.255.111.35"]
"0000"         →  ["0.0.0.0"]
"101023"       →  ["1.0.10.23", "1.0.102.3", "10.1.0.23", "10.10.2.3", "101.0.2.3"]

Constraints

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