LeetCode solutions

3792. Find Invalid IP Addresses

My accepted SQL solution to LeetCode problem 3792, Find Invalid IP Addresses, running in 279ms.

  • Difficulty: Hard
  • SQL
  • Runtime 279ms
  • Memory 0.0B

Read the problem on LeetCode View on GitHub

SQL

Accepted on LeetCode — runtime 279ms, memory 0.0B, accepted 2026-01-02.

sql
SELECT 
    ip,
        COUNT(*) AS invalid_count
        FROM logs
        WHERE 
            -- Has less or more than 4 octets
                LENGTH(ip) - LENGTH(REPLACE(ip, '.', '')) != 3
                    -- Contains numbers greater than 255 or has leading zeros
                        OR NOT ip REGEXP '^((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])\\.){3}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])$'
                        GROUP BY ip
                        ORDER BY invalid_count DESC, ip DESC

Source