Wildcard Bad-Word Filter
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. 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. Only edge wildcards are allowed; patterns containing no letters are ignored.
Replace every letter 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.
Constraints
1 <= badWords.length <= 1000.1 <= message.length <= 100000.- Patterns contain ASCII letters and optional edge
*characters; the message contains printable ASCII and whitespace.