LeetCode June Challenge - Day 16

Problem: LeetCode June Challenge - Day 16

Ideas:

  1. Use python and regular expressions for an easier life.
  2. Check IPv4 integer range using code and not regular expressions, it’s easie.

Solution:

import re

def check_ipv4(IP: str) -> bool:
    regex = "([1-9][0-9]*|0)\.([1-9][0-9]*|0)\.([1-9][0-9]*|0)\.([1-9][0-9]*|0)"
    if not re.fullmatch(regex, IP):
        return False
    return all([0<=int(x)<=255 for x in IP.split('.')])

def check_ipv6(IP: str) -> bool:
    regex = "([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}"
    return bool(re.fullmatch(regex, IP))

class Solution:
    def validIPAddress(self, IP: str) -> str:
        if check_ipv4(IP):
            return "IPv4"
        if check_ipv6(IP):
            return "IPv6"
        return "Neither"