KatInterview

IPv4 Address Checker and Builder

StringsAuto-graded · 23 test cases
MediumAuto-gradedStringsbacktrackingvalidationstring-processing

Last updated

Tests string parsing, validation rules, and backtracking to check and generate valid IPv4 addresses from digit strings.

You are building a network utility that works with IPv4 addresses in two formats: standard dotted-decimal notation and raw digit strings.

Implement a function that performs three related tasks:

Task 1: Verify Dotted-Decimal Format

Given a string in dotted-decimal format, determine whether it represents a valid IPv4 address.

A valid IPv4 address consists of:

  • Exactly four numeric segments separated by three periods
  • Each segment must be a decimal number between 0 and 255 (inclusive)
  • No segment may have leading zeros, except when the segment is exactly "0"
  • Segments contain only digits (no signs, spaces, or other characters)

Task 2: Check Restoration Possibility

Given a string containing only digits (no periods), determine whether it is possible to insert exactly three periods to form a valid IPv4 address under the same rules.

Task 3: Generate All Valid Restorations

Given the same digit-only string, produce every distinct valid IPv4 address that can be formed by inserting three periods. Return the addresses in lexicographically sorted order.

Function Signature

Your function receives two parameters:

  • dotted_format: a string to validate as dotted-decimal IPv4 (Task 1)
  • digit_string: a string of digits to restore into IPv4 addresses (Tasks 2 and 3)

Return a dictionary with three keys:

  • 'is_valid': boolean indicating whether dotted_format is valid
  • 'can_restore': boolean indicating whether digit_string can form any valid IPv4
  • 'restored': list of all valid IPv4 addresses from digit_string, sorted lexicographically

Constraints

  • 1 ≤ len(digit_string) ≤ 12
  • digit_string contains only digit characters ('0'-'9')
  • dotted_format may contain any characters
  • Each segment of a valid IPv4 must be in the range [0, 255]
  • Leading zeros are forbidden except for the value "0" itself

Examples

Example 1

Input: ipv4_check_and_restore('255.255.11.135', '25525511135')

Output: {'is_valid': True, 'can_restore': True, 'restored': ['255.255.11.135', '255.255.111.35']}

Example 2

Input: ipv4_check_and_restore('-1.255.11.135', '00025555')

Output: {'is_valid': False, 'can_restore': False, 'restored': []}

Example 3

Input: ipv4_check_and_restore('01.2.3.4', '123')

Output: {'is_valid': False, 'can_restore': False, 'restored': []}

Hints

Hint 1

Define a helper function to validate whether a single segment meets all the octet rules, then reuse it across all three tasks.

Hint 2

For restoration, use depth-first search to try all ways of partitioning the digit string into four segments, where each segment is 1-3 characters long.

Hint 3

Prune impossible branches early: if k segments remain and r characters are left, you need k ≤ r ≤ 3k to complete a valid address.