Quick Overview

Given a list of IPv4 patterns whose trailing octets may be wildcards and a target address, return the most specific pattern that matches the target, or an empty string. It tests longest-prefix matching over octets with a trie, plus careful handling of ties and the all-wildcard pattern.

Find the longest matching wildcard IP pattern for a target address

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are given a list of IP address `patterns` and a `target` IPv4 address. Return the pattern that matches the target most specifically. Each pattern is a string of four dot-separated octets. Each octet is either a number from 0 to 255 or the wildcard `*`. A pattern matches the target when every non-wildcard octet in the pattern equals the octet in the same position of the target. A wildcard octet matches any value. Among the matching patterns, the longest match is the one with the most leading non-wildcard octets. If several patterns match with the same number of leading non-wildcard octets, return the one that appears first in `patterns`. If no pattern matches, return the empty string `""`. ### Function Signature ```python def longest_matching_pattern(patterns: list[str], target: str) -> str: ``` ### Rules - Wildcards only appear at the end of a pattern: once an octet is `*`, every later octet in that pattern is `*`. So `192.168.*.*` is valid input and `192.*.1.*` is not. - Octets are compared as strings. Numbers are written in plain decimal with no leading zeros, so `1` never appears as `01`. - `*.*.*.*` matches every target with zero matching octets. It counts as a match, so a list that contains it never produces `""`. - Return the pattern exactly as it appears in the input. ### Constraints - `1 <= len(patterns) <= 10^4` - Each pattern and the target consist of exactly 4 dot-separated octets. - Each octet is either `*` or an integer in the range `[0, 255]`. - The target contains no wildcards. ### Examples ```text Input: patterns = ["192.168.*.*", "192.168.1.*", "10.0.0.*", "192.*.*.*"], target = "192.168.1.100" Output: "192.168.1.*" ``` `192.168.1.*` matches three leading octets (192, 168, 1), `192.168.*.*` matches two and `192.*.*.*` matches one. `10.0.0.*` fails on the first octet, so the three-octet match is the answer. ```text Input: patterns = ["10.0.0.1", "10.0.0.*", "10.0.*.*", "10.*.*.*"], target = "10.0.0.1" Output: "10.0.0.1" ``` Every pattern matches, and `10.0.0.1` fixes all four octets, which is the most any pattern can. ```text Input: patterns = ["10.*.*.*", "172.16.5.*"], target = "172.16.4.9" Output: "" ``` `10.*.*.*` fails on the first octet and `172.16.5.*` fails on the third (5 against 4). No pattern matches, so the answer is the empty string.

Overview: Given a list of IPv4 patterns whose trailing octets may be wildcards and a target address, return the most specific pattern that matches the target, or an empty string. It tests longest-prefix matching over octets with a trie, plus careful handling of ties and the all-wildcard pattern.

You are given a list of IPv4 address patterns `patterns` and a target IPv4 address `target`. Return the pattern that matches the target most specifically. Each pattern is a string of four dot-separated octets. Each octet is either a number from 0 to 255 or the wildcard `*`. A pattern matches the target when every non-wildcard octet in the pattern equals the octet in the same position of the target. A wildcard octet matches any value. Among the matching patterns, the longest match is the one with the most leading non-wildcard octets. If several matching patterns have the same number of leading non-wildcard octets, return the one that appears first in `patterns`. If no pattern matches, return the empty string `""`. ### Rules - Wildcards only appear at the end of a pattern: once an octet is `*`, every later octet in that pattern is `*`. So `192.168.*.*` is a valid pattern and `192.*.1.*` is not. - Octets are compared as strings. Numbers are written in plain decimal with no leading zeros, so `1` never appears as `01`. - `*.*.*.*` matches every target with zero matching octets. It counts as a match, so a list that contains it never produces `""`. - Return the pattern exactly as it appears in the input. - `patterns` may contain the same pattern more than once. ### Examples ```text Input: patterns = ["192.168.*.*", "192.168.1.*", "10.0.0.*", "192.*.*.*"], target = "192.168.1.100" Output: "192.168.1.*" ``` `192.168.1.*` matches three leading octets (192, 168, 1), `192.168.*.*` matches two and `192.*.*.*` matches one. `10.0.0.*` fails on the first octet, so the three-octet match is the answer. ```text Input: patterns = ["10.*.*.*", "172.16.5.*"], target = "172.16.4.9" Output: "" ``` `10.*.*.*` fails on the first octet and `172.16.5.*` fails on the third (5 against 4). No pattern matches, so the answer is the empty string. ### Constraints - `1 <= len(patterns) <= 10^4` - Each pattern and the target consist of exactly 4 dot-separated octets. - Each octet is either `*` or an integer in the range `[0, 255]`, written in plain decimal with no leading zeros. - The target contains no wildcards. - Wildcards only appear at the end of a pattern: once an octet is `*`, every later octet in that pattern is `*`.

Constraints

  • 1 <= len(patterns) <= 10^4
  • Each pattern and the target consist of exactly 4 dot-separated octets.
  • Each octet is either * or an integer in the range [0, 255], written in plain decimal with no leading zeros.
  • The target contains no wildcards.
  • Wildcards only appear at the end of a pattern: once an octet is *, every later octet in that pattern is *.

Examples

Input: (['192.168.*.*', '192.168.1.*', '10.0.0.*', '192.*.*.*'], '192.168.1.100')

Expected Output: '192.168.1.*'

Explanation: Source example 1: the three-octet match beats the two-octet and one-octet matches, and 10.0.0.* fails on the first octet.

Input: (['10.0.0.1', '10.0.0.*', '10.0.*.*', '10.*.*.*'], '10.0.0.1')

Expected Output: '10.0.0.1'

Explanation: Source example 2: every pattern matches and the exact four-octet pattern fixes the most octets.

Hints

  1. Because a wildcard can only begin a suffix, the number of leading non-wildcard octets in a pattern is simply how many fixed octets it has.
  2. Every fixed octet has to agree with the target in the same position. Agreeing on the first few is not enough, and 1, 10 and 100 are different octets.
  3. The pattern *.*.*.* is a real match with zero octets, which is different from having no match at all.

Most Specific Matching IPv4 Wildcard Pattern

mediumGoogleSoftware Engineer

You are given a list of IPv4 address patterns patterns and a target IPv4 address target. Return the pattern that matches the target most specifically.

Each pattern is a string of four dot-separated octets. Each octet is either a number from 0 to 255 or the wildcard *. A pattern matches the target when every non-wildcard octet in the pattern equals the octet in the same position of the target. A wildcard octet matches any value.

Among the matching patterns, the longest match is the one with the most leading non-wildcard octets. If several matching patterns have the same number of leading non-wildcard octets, return the one that appears first in patterns. If no pattern matches, return the empty string "".

Rules

  • Wildcards only appear at the end of a pattern: once an octet is *, every later octet in that pattern is *. So 192.168.*.* is a valid pattern and 192.*.1.* is not.
  • Octets are compared as strings. Numbers are written in plain decimal with no leading zeros, so 1 never appears as 01.
  • *.*.*.* matches every target with zero matching octets. It counts as a match, so a list that contains it never produces "".
  • Return the pattern exactly as it appears in the input.
  • patterns may contain the same pattern more than once.

Examples

Input: patterns = ["192.168.*.*", "192.168.1.*", "10.0.0.*", "192.*.*.*"], target = "192.168.1.100"
Output: "192.168.1.*"

192.168.1.* matches three leading octets (192, 168, 1), 192.168.*.* matches two and 192.*.*.* matches one. 10.0.0.* fails on the first octet, so the three-octet match is the answer.

Input: patterns = ["10.*.*.*", "172.16.5.*"], target = "172.16.4.9"
Output: ""

10.*.*.* fails on the first octet and 172.16.5.* fails on the third (5 against 4). No pattern matches, so the answer is the empty string.

Constraints

  • 1 <= len(patterns) <= 10^4
  • Each pattern and the target consist of exactly 4 dot-separated octets.
  • Each octet is either * or an integer in the range [0, 255], written in plain decimal with no leading zeros.
  • The target contains no wildcards.
  • Wildcards only appear at the end of a pattern: once an octet is *, every later octet in that pattern is *.

Examples

Example 1
Input
(['192.168.*.*', '192.168.1.*', '10.0.0.*', '192.*.*.*'], '192.168.1.100')
Output
'192.168.1.*'
Notes
Source example 1: the three-octet match beats the two-octet and one-octet matches, and 10.0.0.* fails on the first octet.
Example 2
Input
(['10.0.0.1', '10.0.0.*', '10.0.*.*', '10.*.*.*'], '10.0.0.1')
Output
'10.0.0.1'
Notes
Source example 2: every pattern matches and the exact four-octet pattern fixes the most octets.

Constraints

  • •1 <= len(patterns) <= 10^4
  • •Each pattern and the target consist of exactly 4 dot-separated octets.
  • •Each octet is either * or an integer in the range [0, 255], written in plain decimal with no leading zeros.
  • •The target contains no wildcards.
  • •Wildcards only appear at the end of a pattern: once an octet is *, every later octet in that pattern is *.

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def longest_matching_pattern(patterns: list[str], target: str) -> str:
Sample call
longest_matching_pattern(['192.168.*.*', '192.168.1.*', '10.0.0.*', '192.*.*.*'], '192.168.1.100')
Language
Case 1
Input
(['192.168.*.*', '192.168.1.*', '10.0.0.*', '192.*.*.*'], '192.168.1.100')
Expected
'192.168.1.*'
Case 2
Input
(['10.0.0.1', '10.0.0.*', '10.0.*.*', '10.*.*.*'], '10.0.0.1')
Expected
'10.0.0.1'

Browse More Questions

Show the approach

Approach

Split the target into its four octets once. Then scan the patterns in input order. For each pattern, walk its octets from left to right: stop at the first *, reject the pattern at the first fixed octet that differs from the target octet in the same position, and otherwise count the fixed octets. Since wildcards only form a suffix, the walk has checked every fixed octet by the time it stops, and the count equals the number of leading non-wildcard octets.

Invariant: after each pattern, best is the earliest pattern seen so far with the highest count among matching patterns, and best_count is that count, or -1 with best equal to the empty string if nothing has matched yet. Starting from -1 lets *.*.*.* (count 0) replace the empty result. A new pattern replaces best only when its count is strictly greater, so among equal counts the first one in patterns is kept, which is the tie rule. When the scan ends, the invariant gives the required answer, or the empty string when no pattern matched.

Edge cases: octets are compared as whole strings, so 1, 10 and 100 are all different and a fixed-prefix string test would be wrong; a pattern that agrees on its first octets but fails a later fixed octet is rejected; duplicate patterns are fine because the returned text is the same either way. Every pattern has exactly four short octets, so each one takes constant work.

Time complexity:
O(n), where n is the number of patterns (each pattern has four octets of at most three characters)
Space complexity:
O(1) extra space beyond the octets of the pattern being checked