Trains the technique from
LeetCode 468Validate IP AddressThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
A switch's admin console asks the operator to type a host address and has to say what kind of address it got. Given the typed text address, return "IPv4" if it is a valid version 4 address, "IPv6" if it is a valid version 6 address, and "Neither" if it is neither.
A valid IPv4 address is four decimal chunks joined by single dots. Each chunk is one to three digits long, stands for a value from 0 to 255, and carries no leading zero unless the chunk is the single digit 0. So 10.0.42.7 is valid, while 10.0.42.256, 10.0.007.7 and 10.0.42 are not.
A valid IPv6 address is eight groups joined by single colons. Each group is one to four hexadecimal digits, taken from 0-9, a-f and A-F. Leading zeros inside a group are fine and upper and lower case are both fine, but an empty group is not allowed and the shorthand that collapses a run of zero groups is not accepted by this console.
Nothing else counts: an address that is short a chunk, carries an extra separator or mixes the two forms is "Neither".
Example 1
Four digit chunks, each between 0 and 255, none of them with a leading zero.
Example 2
The first chunk is written "012", and a chunk longer than one digit may not start with 0.
Example 3
Eight groups, each one to four hexadecimal digits long.
Example 4
Only seven groups are present, and this console does not accept a collapsed run.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def valid_i_p_address(address: str) -> str:public String validIPAddress(String address)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.