Problem 537
Restore IP Addresses
Medium · 20 pts · String, Backtracking
Problem
A valid IPv4 address has exactly four parts separated by dots. Each part is a number from 0 to 255 written without leading zeros (so 0 is fine but 01 and 00 are not). Given a string of digits, insert three dots to form every possible valid IP address, using all the digits in order. Print how many there are, and then the addresses themselves in lexicographic (dictionary) order, one per line.
Input
A single string D made of digits.
Output
Print the number of valid addresses on the first line, then each address on its own line in lexicographic order.
Constraints
1 <= |D| <= 20
Example input
25525511135
Example output
2 255.255.11.135 255.255.111.35
Explanation
25525511135 can be split as 255.255.11.135 or 255.255.111.35. Every other split creates a part above 255 or with a leading zero, so there are 2 addresses.