Smudged Router Label
A repair shop prints each device's network address on a sticker, like 192.168.1.1. The faulty label printer drops every dot, so a sticker reads 19216811, and the technician must work out which addresses it could have been.
A valid address is four numbers separated by dots. Each number is from 0 to 255 and has no leading zeros, so 0 is allowed but 01 and 00 are not.
Your task
Write possibleAddresses(label). label is a string of digits. Return every valid address that becomes label when its three dots are removed, written with dots, like "192.168.1.1".
Use every digit, in order. Return the addresses in any order, each once. If none is possible, return an empty list.
possibleAddresses(label: string) → string[] Any order of the returned list is accepted.
Examples
label = "19216811"
["1.92.168.11", "19.2.168.11", "19.21.68.11", "19.216.8.11", "19.216.81.1", "192.1.68.11", "192.16.8.11", "192.16.81.1", "192.168.1.1"]
The eight digits are split into four numbers. The sticker cannot distinguish the original 192.168.1.1 from other valid splits such as 19.216.81.1, which is also four numbers from 0 to 255. The split 1.921.68.11 is invalid because 921 is over 255.
label = "10203"
["1.0.20.3", "10.2.0.3"]
Five digits in four numbers means exactly one number has two digits. The splits 10.2.0.3 and 1.0.20.3 are valid. The splits 1.02.0.3 and 1.0.2.03 are invalid, because a number cannot start with 0 unless it is just 0.
Constraints
- 0 ≤ label.length ≤ 20
- label contains only the digits 0 to 9.
- Addresses may be returned in any order.
Start with brute force
Choose 3 of the n − 1 gaps between digits for the dots and check the four pieces. For 12 digits that is only 165 choices, so it is not slow, but it is rigid and wasteful.
It checks splits whose first piece is 1921 or 05, which can never work, and its three nested loops are hard-wired to four parts. Building the address piece by piece and rejecting a bad piece immediately does less work and handles any number of parts.
Hints
0 of 4 shown- Hint 1
In
"10203", the first number can be only 1, 2 or 3 digits long. Which of1,10and102are allowed? - Hint 2
Once the first number is fixed, the rest is the same puzzle on a shorter string, with three numbers to place. That is a recursive structure.
- Hint 3
Write
place(start), trying a piece of length 1, 2 or 3 atstart. Skip a piece that runs past the end, has a leading zero, or is over 255. After four pieces, keep the address only if every digit is used. - Hint 4
Add the piece to a list of parts, recurse on
start + length, then remove the piece again. A piece that has a leading zero or is over 255 only gets worse when it is longer, so stop trying longer ones. Reject a label longer than 12 digits immediately.
Walkthrough and solution
Unlocks when you pass every test. Stuck for a while? Looking is fine too, as long as you then close it and write the code yourself.
How to solve it
An address is four choices in a row, where each choice is whether the next number takes 1, 2 or 3 digits. Each choice either breaks a rule immediately or leaves a smaller version of the same puzzle, so the recursion has three branches per level.
Trace "10203":
- Take
1as the first piece, which leaves0203. The second piece must be0, because02has a leading zero, which leaves203.- If the third piece is
2, the fourth piece would have to be03, which has a leading zero and is rejected. - If the third piece is
20and the fourth is3, record1.0.20.3. - If the third piece is
203, nothing is left for the fourth.
- If the third piece is
- Take
10as the first piece, which leaves203. The pieces2,0and3then give10.2.0.3, and longer pieces leave too few digits. - Take
102as the first piece, which leaves03. The second piece is0and the third is3, and no digit is left for the fourth, so this branch is a dead end.
There are two addresses. Be careful with the leading-zero rule: 0 alone is allowed, so reject a piece that is longer than 1 digit and starts with 0, not every piece that starts with 0.
function possibleAddresses(label) {
const results = [];
const parts = [];
function place(start) {
if (parts.length === 4) {
if (start === label.length) results.push(parts.join('.'));
return;
}
for (let len = 1; len <= 3; len++) {
if (start + len > label.length) break;
const piece = label.slice(start, start + len);
if (len > 1 && piece[0] === '0') break; // no leading zeros
if (Number(piece) > 255) break; // longer is bigger still
parts.push(piece);
place(start + len);
parts.pop();
}
}
if (label.length <= 12) place(0); // 4 parts of at most 3 digits
return results;
}def possible_addresses(label):
results = []
parts = []
def place(start):
if len(parts) == 4:
if start == len(label): # all digits used
results.append('.'.join(parts))
return
for length in (1, 2, 3):
if start + length > len(label):
break
piece = label[start:start + length]
if length > 1 and piece[0] == '0':
break # no leading zeros
if int(piece) > 255:
break # longer pieces are bigger still
parts.append(piece)
place(start + length)
parts.pop()
if len(label) <= 12: # 4 parts of at most 3 digits
place(0)
return resultsRun your code to see results here. Run checks the examples. Submit checks every test, including hidden edge cases.