Wildcard Bad-Word Filter
Updated
Problem statement
Replace bad words in a message while preserving every punctuation mark and whitespace character.
A candidate word is a maximal run of ASCII letters and digits. Matching ignores case. A pattern without * must equal the whole word. A leading * matches any prefix, a trailing * matches any suffix, and both match any containing word; each wildcard stands for zero or more letters or digits. Only edge wildcards are allowed; patterns containing no letters or digits are ignored.
Replace every letter and digit of a matching word with *.
Function
filterBadWords(badWords: String[], message: String) → StringExamples
Example 1
badWords = ["jerk*","*lame*"]message = "Stop it, you jerkwad! I remain blameless!"return = "Stop it, you *******! I remain *********!"The prefix and contains patterns match while punctuation remains.
Example 2
badWords = ["bad","*tail","head*"]message = "BAD cocktail headwind badge"return = "*** ******** ******** badge"Matching ignores case and exact bad does not match badge.
Example 3
badWords = ["*","**"]message = "keep all!"return = "keep all!"Wildcard-only patterns are ignored and repeated spaces stay unchanged.
Example 4
badWords = ["bot*","*404"]message = "Bot42 hit page404, then 404 again; robot2 stayed."return = "***** hit *******, then *** again; robot2 stayed."Words are runs of letters and digits, so Bot42 matches bot*, and page404 and 404 both match *404, where the wildcard covers zero characters for 404. robot2 does not start with bot.
Constraints
1 <= badWords.length <= 1000.1 <= message.length <= 100000.- Each pattern is either one or more
*characters, or ASCII letters and digits with an optional*at either edge. - The message contains printable ASCII characters and whitespace (space, tab, newline or carriage return).
More Sentry interview questions
Similar questions at other companies
- Basic Calculator IITypeSafe AI · OA
- Find a String Within One Mismatch of AllBarclays · OABarclays interview questions
- Maximum String OperationsMicrosoft · OAMicrosoft interview questions
- Perfect SubstringAXQ Capital · OA
- Reliable LLM Call with Independent Retry BudgetsStripe · OAStripe interview questions
- Maximum Equal-Bit Pairs in a SubsequenceAmazon · OA
- Minimum No-Repeat Segments After One Character RemovalSalesforce · OASalesforce interview questions
- Count Subarrays Matching a Comparison PatternTikTok · OATikTok interview questions
- Earliest Time to Schedule a MeetingMathWorks · OAMathWorks interview questions
- Earliest Meeting Start on a 24-Hour ClockIMC Trading · OAIMC Trading interview questions