Validate and restore IPv4 addresses
IPv4 Address Validation and Restoration
This problem involves designing and implementing functions to validate and restore IPv4 addresses based on specific rules.
Part A: Validate Dotted IPv4
Write a function isValidIPv4(s) that returns true if s is a valid dotted-decimal IPv4 address and false otherwise.
Requirements for a valid IPv4 address:
- Exactly four decimal octets separated by three dots.
- Each octet must consist of digits only and represent an integer in the range [0, 255].
- No leading zeros are allowed unless the octet itself is exactly "0".
- No plus/minus signs, whitespace, or extra characters are permitted.
Examples:
"255.255.11.135"->true"-1.255.11.135"->false"01.2.3.4"->false"256.0.0.1"->false
Part B: Validate Undelimited IPv4 Possibility
Given a string s consisting only of digits (no dots), return true if it can be segmented into a valid IPv4 address by inserting three dots, adhering to the same rules as Part A. Return false otherwise.
Examples:
"25525511135"->true(e.g.,255.255.11.135)"0002555"->true(e.g.,0.0.255.55)"123"->false
Part C: Restore All Valid IPv4 Addresses
Given a string s of digits, return all unique valid IPv4 addresses that can be formed by inserting three dots under the rules described in Part A. The addresses can be returned in any order, but duplicates should be excluded.
Example:
"25525511135"->["255.255.11.135", "255.255.111.35"]
Constraints
- For Parts B and C:
1 <= s.length <= 12. - Aim for an efficient solution, typically involving DFS/backtracking for Parts B and C.
AI interview prep
Core dilemma
The central engineering conflict is balancing the strict parsing rules for individual IPv4 octets with the combinatorial challenge of exploring all possible segmentations of a digit string. The candidate must demonstrate both meticulous string manipulation and efficient search.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Validate and restore IPv4 addresses
IPv4 Address Validation and Restoration
This problem involves designing and implementing functions to validate and restore IPv4 addresses based on specific rules.
Part A: Validate Dotted IPv4
Write a function isValidIPv4(s) that returns true if s is a valid dotted-decimal IPv4 address and false otherwise.
Requirements for a valid IPv4 address:
- Exactly four decimal octets separated by three dots.
- Each octet must consist of digits only and represent an integer in the range [0, 255].
- No leading zeros are allowed unless the octet itself is exactly "0".
- No plus/minus signs, whitespace, or extra characters are permitted.
Examples:
"255.255.11.135"->true"-1.255.11.135"->false"01.2.3.4"->false"256.0.0.1"->false
Part B: Validate Undelimited IPv4 Possibility
Given a string s consisting only of digits (no dots), return true if it can be segmented into a valid IPv4 address by inserting three dots, adhering to the same rules as Part A. Return false otherwise.
Examples:
"25525511135"->true(e.g.,255.255.11.135)"0002555"->true(e.g.,0.0.255.55)"123"->false
Part C: Restore All Valid IPv4 Addresses
Given a string s of digits, return all unique valid IPv4 addresses that can be formed by inserting three dots under the rules described in Part A. The addresses can be returned in any order, but duplicates should be excluded.
Example:
"25525511135"->["255.255.11.135", "255.255.111.35"]
Constraints
- For Parts B and C:
1 <= s.length <= 12. - Aim for an efficient solution, typically involving DFS/backtracking for Parts B and C.