Skip to content
BytePatterns

Dotted Addresses From Digits

MediumBacktracking#backtracking#pruning~30m

Problem

A log file lost the dots from its network addresses, leaving only a string of digits. An address is four numbers from 0 to 255 joined by dots, and a number never has a leading zero unless it is exactly 0. Return every address that could have produced the digit string, keeping the digits in order and using all of them. Any order of the results is accepted.

Examples

Input:  s = "25525511135"
Output: ["255.255.11.135", "255.255.111.35"]
Input:  s = "101023"
Output: ["1.0.10.23", "1.0.102.3", "10.1.0.23", "10.10.2.3", "101.0.2.3"]
Why:    "1.01.0.23" is not listed, since 01 has a leading zero
Input:  s = "0000"
Output: ["0.0.0.0"]
Why:    edge case, each part must be a single 0

Hints

0 / 3

Stuck on the idea rather than the code? The Decision Tree covers it.