Advertisement

Restore IP Addresses - LeetCode 93 Solution

Restore IP Addresses - Complete Solution Guide

Restore IP Addresses is LeetCode problem 93, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

A valid IP address consists of exactly four integers separated by single dots. Each integer is between 0 and 255 ( inclusive ) and cannot have leading zeros. For example, "0.1.2.201" and "192.168.1.1" are valid IP addresses, but "0.011.255.245" , "192.168.1.312" and "[email protected]" are invalid IP addresses. Given a string s containing only digits, return all possible valid IP addresses that can be formed by inserting dots into s . You are not allowed to reorder or remove any digits in s . You may

Detailed Explanation

The problem asks us to take a string `s` consisting only of digits and determine all possible valid IP addresses that can be formed by inserting exactly three dots into the string. A valid IP address has four parts, each being an integer between 0 and 255 (inclusive), and no part can have leading zeros unless it is exactly zero. We are not allowed to reorder or remove any digits from the input string. The output should be a list of strings, each representing a valid IP address formed from the input string.

Solution Approach

The provided solutions use a backtracking approach to explore all possible ways to split the input string `s` into four parts. The `backtrack` function recursively explores different combinations of splitting the string by choosing the length of each part (1, 2, or 3). For each potential part, it checks if the part is a valid integer (between 0 and 255 and without leading zeros). If a valid split into four parts is found, it is added to the result list. The backtracking step involves appending the current valid part to a list representing potential valid ip address, recursively calling backtrack function with the next starting index and updated partial ip, and removing the part from the list before returning to the previous state.

Step-by-Step Algorithm

  1. Step 1: Check if the input string length is within the valid range (4 to 12 inclusive). If not, return an empty list.
  2. Step 2: Define a recursive `backtrack` function with arguments: `start_index` (the starting index of the current part), and `current_parts` (a list of strings representing the parts of the IP address built so far).
  3. Step 3: In the `backtrack` function, if `current_parts` has 4 elements:
  4. - If `start_index` has reached the end of the string, join the parts with dots and add it to the result.
  5. - Return.
  6. Step 4: Iterate through possible lengths (1, 2, and 3) for the next part of the IP address.
  7. Step 5: For each length:
  8. - Check if the current `start_index` plus the length exceeds the string length. If so, break the loop.
  9. - Extract the substring `part` of the given length from `start_index`.
  10. - Check for invalid parts: leading zeros (if length > 1 and `part[0]` is '0') and if the integer value of `part` exceeds 255.
  11. - If the part is valid, add it to `current_parts`.
  12. - Call `backtrack` recursively with the updated `start_index` and `current_parts`.
  13. - Remove the last added part from `current_parts` (backtracking).
  14. Step 6: Call the `backtrack` function initially with `start_index` as 0 and an empty list for `current_parts`.
  15. Step 7: Return the result list of valid IP addresses.
  16. Important: Free any dynamically allocated memory, especially in C, to prevent memory leaks.

Key Insights

  • Insight 1: Backtracking is a natural fit since we need to explore different possibilities for splitting the string into four parts.
  • Insight 2: Pruning is crucial to avoid unnecessary exploration. We can prune branches if the current part is invalid (e.g., leading zero, greater than 255) or if we've already used more than 4 parts or if the remaining string is not sufficient to form the rest of the ip address.
  • Insight 3: The length of each part of IP address can be only 1, 2 or 3. This limits the search space.

Complexity Analysis

Time Complexity: O(3^n)

Space Complexity: O(n)

Topics

This problem involves: String, Backtracking.

Companies

Asked at: Adobe, Amazon, Apple, Arista Networks, Autodesk, Cisco, Meta, Microsoft, Oracle, Palo Alto Networks, StackAdapt, TikTok, Visa, Zoho.