Pick the Tightest Wildcard Mask for a Bit String
StringsAuto-graded · 21 test casesLast updated
Scan a list of 0/1/? templates, keep those that fit a bit string, and return the one with the fewest wildcards, breaking ties by earliest position.
Tightest Template Match
You are given a list of templates and a single target bit string.
- Each template is a string made of the characters
'0','1'and'?'. - The target is a string made only of
'0'and'1'.
A template fits the target when both of the following hold:
- The template and the target have the same length.
- At every position, the template character is either
'?'or equal to the target character at that position.
Among all templates that fit, return the tightest one, chosen by these rules in order:
- Prefer the template with the fewest
'?'characters. - If several templates are still tied, prefer the one that appears earliest in the input list.
If no template fits the target, return None.
The returned value is the template string itself, exactly as given in the list.
Constraints
0 <= len(templates) <= 10,000- Templates contain only the characters
'0','1'and'?'. - The target contains only
'0'and'1'. - Templates may have different lengths from the target; those never fit.
- Duplicate templates may appear in the list.
- The function returns a
str, orNonewhen nothing fits.
Examples
Example 1
Input: tightest_template(['0?1', '1?1', '011'], '011')
Output: '011'
Example 2
Input: tightest_template(['0?0', '?10'], '000')
Output: '0?0'
Example 3
Input: tightest_template(['???', '101', '1?1', '101'], '101')
Output: '101'
Hints
Hint 1
Each template can be judged independently of all the others, so a single pass over the list is enough.
Hint 2
A template of a different length can never fit. Check the length explicitly rather than relying on a pairwise loop that stops at the shorter string.
Hint 3
Rank fitting templates by the pair (number of '?', index in list). Replace your current best only when the new pair is strictly smaller, so earlier templates win ties.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Every template can be evaluated on its own, and the ranking is a strict ordering on the pair (wildcard count, list index). So one pass over the list is enough.
- Skip any template whose length differs from the target.
- Walk the template and target together. Count
'?'characters, and stop immediately if a fixed character ('0'or'1') differs from the target character. - If the template fits, form the key
(wildcards, index). If it is strictly smaller than the best key seen so far, remember this template. - After the scan, return the remembered template, or
Noneif none fit.
Why it is correct
- Fit test: The inner loop rejects on the first fixed-character mismatch and accepts only if every position passes, which is exactly the definition of fitting. The explicit length check is needed because
zipsilently truncates to the shorter string. - Ranking: The desired answer is the fitting template minimizing
(wildcards, index)under tuple ordering. Since every template is visited and the best is replaced only on a strictly smaller key, the stored key at the end is the global minimum. - Ties: Indices are distinct and visited in increasing order, so a later template with an equal wildcard count has a larger key and never displaces an earlier one.
Complexity
- Time: O(N * L), where N is the number of templates and L is the target length. Each template costs at most L comparisons, often fewer because of early exit.
- Space: O(1) extra.
Reference solution
from typing import List, Optional
def tightest_template(templates: List[str], target: str) -> Optional[str]:
best_template = None
best_key = None
for index, template in enumerate(templates):
if len(template) != len(target):
continue
wildcards = 0
fits = True
for t_ch, g_ch in zip(template, target):
if t_ch == '?':
wildcards += 1
elif t_ch != g_ch:
fits = False
break
if fits:
key = (wildcards, index)
if best_key is None or key < best_key:
best_key = key
best_template = template
return best_template