Given a list of bad words bad_words and a message string, identify whether the message contains any prohibited words, even when they are obfuscated with leet speak substitutions.
Leet speak substitutions map certain characters to letters as follows:
| Character | Letter |
|---|
0 | o |
1 | i |
3 | e |
4 | a |
5 | s |
7 | t |
@ | a |
$ | s |
! | i |
The digit 1 is sometimes read as l too, but for this problem it always maps to i.
The check is case-insensitive. Return a list of the bad words found in the message, in the order they appear in bad_words. If a bad word appears several times in the message, include it only once. A bad word counts as found if it appears as a substring of any whitespace-separated word in the normalized message.
Function Signature
def filter_bad_words(bad_words: list[str], message: str) -> list[str]:
Rules
-
Normalize the message by lowercasing it and then replacing every character from the table with its letter. All other characters, including punctuation, stay as they are.
-
Split the normalized message on whitespace. A bad word is found when it is a substring of at least one of those words, so a match never spans a space.
-
Each bad word appears at most once in the result, at its first position in
bad_words
, even if the list contains it more than once.
-
Return an empty list when no bad word is found.
Constraints
-
1 <= len(bad_words) <= 100
-
1 <= len(bad_words[i]) <= 50
-
1 <= len(message) <= 10000
-
bad_words[i]
consists of lowercase English letters.
-
message
consists of printable ASCII characters.
Examples
Input: bad_words = ["hate", "kill", "spam"], message = "I h4t3 this and want to k!ll sp@m"
Output: ["hate", "kill", "spam"]
After normalization the message reads i hate this and want to kill spam. All three bad words appear as normalized words.
Input: bad_words = ["bad", "evil", "mean"], message = "This is a b@d m3$$4g3 from 3v!l people"
Output: ["bad", "evil"]
After normalization the message reads this is a bad message from evil people. "bad" and "evil" match whole words, and no word contains "mean".
Input: bad_words = ["troll", "spam"], message = "Sp4m and m0re SP@MMING from a tr0ll"
Output: ["troll", "spam"]
The normalized message is spam and more spamming from a troll. "spam" occurs twice (once inside "spamming") but is reported once, and the result follows the order of bad_words, so "troll" comes first.