Link: https://leetcode.com/problems/restore-ip-addresses/
Solution:
Topics: DFS
Intuition
This is a nice little recursion problem. It should be clear that recursion should be used here because of the result is built up in a decision tree.
For example:
s = '11111'
''
/ \
1. 11.
/ \ / \
1.1. 1.11. 11.1. 11.11.
...
After this, it’s simply a matter of setting up the recursion correctly to ensure that the leaf nodes are valid ip’s. The base case is if all digits in s
have been used and if the length of the built string is equal to the length of s
plus 3 (three dots):
To branch off from curr
, we simply try add each subarray from s[i:i+1]
to s[i:i+3
] to curr
and shift to the correct index. The subarray must be validated as a legal segment of an ip before the recursion can continue. Namely, if the value of segment is smaller than 256 and if it is a legal integer (no leading zeros).
We can also early exit the recursion if the remaining number of digits is less than the number of dots still required, but this is kind of overkill IMO.
Implementation
Mnemonic
Try 3 digits, build the tree.
Visual
Review 1
This is really a back tracking problem. The above implementation is not great and does not actually use back tracking properly (arguably at all). Here is a much better implementation:
Implementation (back tracking)