You are given a list of book titles and a user query string. Return all titles that are relevant to the query.
A title is considered relevant if any token in the title matches the query under at least one of these rules:
-
Exact match
: the token equals the query, ignoring case.
-
Autocomplete match
: the token starts with the query, ignoring case.
-
Reordered-letter match
: the token is an anagram of the query, ignoring case. For example,
culb
matches
Club
because they contain the same letters.
Requirements:
-
Tokenize each title into words by splitting on spaces.
-
Matching is case-insensitive.
-
Return each matching title at most once.
-
Preserve the original input order of titles.
Example:
-
Titles:
["The Luck of Club"]
-
Query:
"Luck"
-> return
["The Luck of Club"]
-
Query:
"luc"
-> return
["The Luck of Club"]
because
luc
is a prefix of
Luck
-
Query:
"culb"
-> return
["The Luck of Club"]
because
culb
is an anagram of
Club
Design and implement an efficient function for this search problem. Discuss the trade-offs between preprocessing the titles and answering queries quickly.