Filter Names Absent From a Blocklist After Cleanup
Arrays & HashingAuto-graded · 21 test casesLast updated
Return the items of one string list whose trimmed, lowercased form is missing from a second list, keeping original spelling, order, and duplicates; tests hash sets and normalization.
You are given two lists of strings: candidates and blocklist.
Two strings are considered the same if they are equal after this cleanup:
- Remove leading and trailing whitespace.
- Convert all letters to lowercase.
Return a new list containing every string from candidates whose cleaned form does not equal the cleaned form of any string in blocklist.
Rules for the output:
- Return the strings exactly as they appear in
candidates(original spacing and letter case). Cleanup is used only for comparison. - Keep the original relative order of
candidates. - Do not remove duplicates: if several kept entries are identical (or equivalent after cleanup), every one of them appears in the result.
- Repeated entries in
blocklisthave no extra effect. - If
candidatesis empty, the result is an empty list. Ifblocklistis empty, every entry ofcandidatesis kept. - A string consisting only of whitespace cleans to the empty string, and is removed only if
blocklistcontains an entry that also cleans to the empty string.
Constraints
0 <= len(candidates), len(blocklist) <= 10^5- Each string has length at most 100.
- Use Python's
strip()andlower()semantics for the cleanup. - Aim for roughly linear time in the total size of the input.
Examples
Example 1
Input: filter_blocked_names([' Apple ', 'BANANA', 'apple', 'Cherry'], ['apple', ' banana '])
Output: ['Cherry']
Example 2
Input: filter_blocked_names(['One', 'Two', 'Three'], [' four ', 'TWO'])
Output: ['One', 'Three']
Example 3
Input: filter_blocked_names([], ['x'])
Output: []
Hints
Hint 1
Cleanup should be treated as a comparison key only; the output must contain the original, untouched strings.
Hint 2
Checking membership in a list for each candidate is quadratic. Pre-compute the cleaned forms of the blocklist once and store them in a hash set.
Hint 3
Make one pass over the candidates, clean each one, and keep it if its key is not in the set. This automatically preserves order and duplicates.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Treat the cleanup (strip() then lower()) as a key function used only for comparison.
- Build a hash set containing the cleaned form of every string in
blocklist. - Walk through
candidatesonce, in order. For each string, compute its cleaned form and keep the original string only if that form is not in the set.
Why it is correct
A string x from candidates must be dropped exactly when some blocklist entry has the same cleaned form as x. The set holds precisely the cleaned forms of all blocklist entries, so clean(x) in set is exactly that condition. Because we append the original string during a left-to-right scan, the output keeps the original text, order, and duplicates. Duplicates in blocklist simply collapse in the set. Empty inputs fall out naturally: an empty candidates gives an empty result, and an empty blocklist gives an empty set, so everything is kept. Whitespace-only strings clean to "", which is blocked only if the set contains "".
Complexity
Let n = len(candidates), m = len(blocklist), and L be the maximum string length.
- Time: O((n + m) * L) on average, since each string is cleaned once and set operations are O(1) on average beyond hashing.
- Space: O(m * L) for the set, plus the output list.
A naive approach that searches a list of blocked strings for each candidate would cost O(n * m * L).
Reference solution
from typing import List
def filter_blocked_names(candidates: List[str], blocklist: List[str]) -> List[str]:
blocked = {s.strip().lower() for s in blocklist}
return [s for s in candidates if s.strip().lower() not in blocked]