Show problems as
Classic problem #93MediumStringBacktracking

Restore IP Addresses

Real-world version: Panel Address Splits
Tracked in this browser only
Read Restore IP Addresses on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

A relay panel is identified by four numeric fields joined with -, for example 12-7-0-240. Each field is held in a single byte, so its value runs from 0 to 255, and each field is written in its shortest form: either the single digit 0, or a number whose first digit is not 0.

A technician copied a panel address down without the separators, leaving only digits, a string of decimal digits.

Return every panel address that produces exactly digits once its three separators are dropped, in any order. Return an empty list when no address produces it.

Examples

Example 1

Input
digits = "01203"
Output
["0-12-0-3", "0-1-20-3"]

Both addresses reproduce 01203 when the separators come out, and each of their fields is either a lone 0 or a number that does not open with 0.

Example 2

Input
digits = "10203"
Output
["10-2-0-3", "1-0-20-3"]

Every field shown lies between 0 and 255 and none opens with a 0 it does not need, so both addresses qualify.

Example 3

Input
digits = "9"
Output
[]

Four fields need at least four digits between them, and only one digit was copied down.

Constraints

  • 1 <= digits.length <= 20
  • digits consists of decimal digits only

The values you return may be in any order.

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def panel_addresses(digits: str) -> list[str]:
Java
public List<String> panelAddresses(String digits)