Problem
Submissions

Solution: Restore IP Addresses

Statement

Naive approach

The naive approach would be to check all possible positions of the dots. Because an IP address requires inserting three dots, and if the input string contains 1212 digits, then there are 1111 possible positions between the digits where a dot can be placed. So, we initially have 1111 choices for the first dot, then 1010 choices for the second dot, and 99 choices for the third dot. This results in at most 11×10×9=99011 × 10 × 9 = 990 combinations to validate in the worst case.

We can introduce three nested loops to try all possible positions for placing the dots: the outer loop iterates through every valid position for the first dot (up to position 11), the middle loop tries all possible positions for the second dot that come after the first, and the inner loop attempts every possible position for the third dot following the second. After placing the dots, each resulting substring between the dots must be validated as a valid number, meaning it should be between 0 and 255 (inclusive) and must not have leading zeros.

Overall, this approach has a time complexity of O(n3)O(n^3) due to the three nested loops required to place the dots. The space complexity is O(1)O(1), since we are not using any additional data structures beyond a constant amount of space.

Optimized approach using backtracking

Each numeric segment in the IP address is known as an octet, and each octet can have 1 to 3 digits.

Example IP Address: 192.168.0.1192.168.0.1

Problem
Submissions

Solution: Restore IP Addresses

Statement

Naive approach

The naive approach would be to check all possible positions of the dots. Because an IP address requires inserting three dots, and if the input string contains 1212 digits, then there are 1111 possible positions between the digits where a dot can be placed. So, we initially have 1111 choices for the first dot, then 1010 choices for the second dot, and 99 choices for the third dot. This results in at most 11×10×9=99011 × 10 × 9 = 990 combinations to validate in the worst case.

We can introduce three nested loops to try all possible positions for placing the dots: the outer loop iterates through every valid position for the first dot (up to position 11), the middle loop tries all possible positions for the second dot that come after the first, and the inner loop attempts every possible position for the third dot following the second. After placing the dots, each resulting substring between the dots must be validated as a valid number, meaning it should be between 0 and 255 (inclusive) and must not have leading zeros.

Overall, this approach has a time complexity of O(n3)O(n^3) due to the three nested loops required to place the dots. The space complexity is O(1)O(1), since we are not using any additional data structures beyond a constant amount of space.

Optimized approach using backtracking

Each numeric segment in the IP address is known as an octet, and each octet can have 1 to 3 digits.

Example IP Address: 192.168.0.1192.168.0.1