Quick Overview

Replace every case-insensitive occurrence of a list of banned words inside a chat message with asterisks of equal length, keeping all other characters and their case intact. It tests substring search that finds overlapping matches and careful handling of characters covered by more than one banned word.

Censor banned words in a chat message with asterisks, matching case-insensitively and across overlaps

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are building a chat filter for a messaging application. Given a string `message` and a list of strings `banned_words`, return a new string where every occurrence of any banned word in `message` is replaced with asterisks (`*`) of the same length. The matching is case-insensitive for detection, but the original casing and structure of `message` (including spaces) must be preserved for all non-banned characters. A banned word can appear anywhere in `message`, including as a substring of a larger word, and all overlapping and non-overlapping occurrences must be censored. If a character belongs to any banned word occurrence, replace it with `*`. Otherwise, keep it unchanged. ### Function Signature ```python def filter_message(message: str, banned_words: list[str]) -> str: ``` ### Rules - A banned word matches a substring of `message` when the two are equal after lowercasing both. - Occurrences may overlap each other, and an occurrence of one banned word may overlap an occurrence of another. Every character covered by at least one occurrence becomes `*`. - Banned words contain only letters, so an occurrence never covers a space. - The output has the same length as `message`. Characters outside every occurrence keep their original case. ### Constraints - `1 <= len(message) <= 10^4` - `1 <= len(banned_words) <= 100` - `1 <= len(banned_words[i]) <= 50` - `message` consists of English letters (uppercase or lowercase) and spaces. - `banned_words[i]` consists of lowercase English letters only. ### Examples ```text Input: message = "hello world this is a test", banned_words = ["world", "test"] Output: "hello ***** this is a ****" ``` `"world"` starts at index 6 and `"test"` starts at index 22. Each is replaced with asterisks of the same length, and everything else is unchanged. ```text Input: message = "the quick brown fox", banned_words = ["cat", "dog"] Output: "the quick brown fox" ``` Neither `"cat"` nor `"dog"` appears in the message, so it is returned unchanged. ```text Input: message = "Banana Bandana", banned_words = ["ana"] Output: "B***** Band***" ``` After lowercasing, `"ana"` occurs at indices 1 and 3 (overlapping, covering indices 1 to 5) and at index 11 inside `"bandana"` (covering 11 to 13). The uncovered letters keep their case, so the capital `B`s stay.

Overview: Replace every case-insensitive occurrence of a list of banned words inside a chat message with asterisks of equal length, keeping all other characters and their case intact. It tests substring search that finds overlapping matches and careful handling of characters covered by more than one banned word.

You are building a chat filter for a messaging application. Given a string `message` and a list of strings `banned_words`, return a new string in which every occurrence of any banned word in `message` is replaced with asterisks (`*`) of the same length. Detection is case-insensitive: a banned word matches a substring of `message` when the two are equal after lowercasing both. A banned word can appear anywhere in `message`, including as a substring of a larger word, and every occurrence must be censored. Occurrences may overlap each other, and an occurrence of one banned word may overlap an occurrence of another. If a character belongs to at least one banned word occurrence, it becomes `*`. Every other character keeps its original value and case, and spaces are preserved. Banned words contain only letters, so an occurrence never covers a space. The returned string has the same length as `message`. ### Examples ```text Input: message = "hello world this is a test", banned_words = ["world", "test"] Output: "hello ***** this is a ****" ``` `"world"` starts at index 6 and `"test"` starts at index 22. Each is replaced with asterisks of the same length, and everything else is unchanged. ```text Input: message = "Banana Bandana", banned_words = ["ana"] Output: "B***** Band***" ``` After lowercasing, `"ana"` occurs at indices 1 and 3 (overlapping, together covering indices 1 to 5) and at index 11 inside `"bandana"` (covering 11 to 13). The uncovered letters keep their case, so both capital `B`s stay. ### Constraints - `1 <= len(message) <= 10^4` - `1 <= len(banned_words) <= 100` - `1 <= len(banned_words[i]) <= 50` - `message` consists of English letters (uppercase or lowercase) and spaces. - `banned_words[i]` consists of lowercase English letters only.

Constraints

  • 1 <= len(message) <= 10^4
  • 1 <= len(banned_words) <= 100
  • 1 <= len(banned_words[i]) <= 50
  • message consists of English letters (uppercase or lowercase) and spaces.
  • banned_words[i] consists of lowercase English letters only.

Examples

Input: ('hello world this is a test', ['world', 'test'])

Expected Output: 'hello ***** this is a ****'

Explanation: Source example 1: 'world' at index 6 and 'test' at index 22 (the last four characters) are censored and the spaces stay. Message length 26.

Input: ('the quick brown fox', ['cat', 'dog'])

Expected Output: 'the quick brown fox'

Explanation: Source example 2: no banned word occurs, so the message (length 19) is returned unchanged.

Hints

  1. Detection ignores case, while the output keeps the original characters. Compare against a lowercased view, then build the answer from message itself.
  2. A match may start inside another match, of the same word or a different one, and both still count.
  3. For each position, all you need to know is whether at least one occurrence covers it. The order in which occurrences are found does not affect the result.

Censor Banned Words in a Chat Message

mediumGeneralSoftware Engineer

You are building a chat filter for a messaging application. Given a string message and a list of strings banned_words, return a new string in which every occurrence of any banned word in message is replaced with asterisks (*) of the same length.

Detection is case-insensitive: a banned word matches a substring of message when the two are equal after lowercasing both. A banned word can appear anywhere in message, including as a substring of a larger word, and every occurrence must be censored. Occurrences may overlap each other, and an occurrence of one banned word may overlap an occurrence of another.

If a character belongs to at least one banned word occurrence, it becomes *. Every other character keeps its original value and case, and spaces are preserved. Banned words contain only letters, so an occurrence never covers a space. The returned string has the same length as message.

Examples

Input: message = "hello world this is a test", banned_words = ["world", "test"]
Output: "hello ***** this is a ****"

"world" starts at index 6 and "test" starts at index 22. Each is replaced with asterisks of the same length, and everything else is unchanged.

Input: message = "Banana Bandana", banned_words = ["ana"]
Output: "B***** Band***"

After lowercasing, "ana" occurs at indices 1 and 3 (overlapping, together covering indices 1 to 5) and at index 11 inside "bandana" (covering 11 to 13). The uncovered letters keep their case, so both capital Bs stay.

Constraints

  • 1 <= len(message) <= 10^4
  • 1 <= len(banned_words) <= 100
  • 1 <= len(banned_words[i]) <= 50
  • message consists of English letters (uppercase or lowercase) and spaces.
  • banned_words[i] consists of lowercase English letters only.

Examples

Example 1
Input
('hello world this is a test', ['world', 'test'])
Output
'hello ***** this is a ****'
Notes
Source example 1: 'world' at index 6 and 'test' at index 22 (the last four characters) are censored and the spaces stay. Message length 26.
Example 2
Input
('the quick brown fox', ['cat', 'dog'])
Output
'the quick brown fox'
Notes
Source example 2: no banned word occurs, so the message (length 19) is returned unchanged.

Constraints

  • •1 <= len(message) <= 10^4
  • •1 <= len(banned_words) <= 100
  • •1 <= len(banned_words[i]) <= 50
  • •message consists of English letters (uppercase or lowercase) and spaces.
  • •banned_words[i] consists of lowercase English letters only.

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def filter_message(message: str, banned_words: list[str]) -> str:
Sample call
filter_message('hello world this is a test', ['world', 'test'])
Language
Case 1
Input
('hello world this is a test', ['world', 'test'])
Expected
'hello ***** this is a ****'
Case 2
Input
('the quick brown fox', ['cat', 'dog'])
Expected
'the quick brown fox'

Browse More Questions

Show the approach

Approach

Lowercase a copy of message once; it holds only English letters and spaces, so its length does not change and index i still refers to the same character. For each distinct banned word of length m, find every start index s where the lowercased message equals the word on [s, s + m). After each match the search resumes at s + 1, so a match that starts inside an earlier one is still found. Each occurrence adds +1 at s and -1 at s + m in a difference array of size n + 1. A running prefix sum over that array then gives, at every index, the number of found occurrences that cover it: a positive count writes '*', and a zero count copies the original character with its case.

Invariant: after adding diff[i], the running sum equals the number of occurrences [s, s + m) with s <= i < s + m. Correctness: the search visits every start position of every distinct word, so every occurrence is recorded, and a character is starred exactly when at least one occurrence covers it, which is the rule in the statement. A repeated entry in banned_words would record the same occurrences again and change no output character, so duplicates are skipped.

Edge cases: a banned word longer than the message has no start position; a partial match cut off by the end of the message is not an occurrence; spaces are never covered because banned words contain only letters; a message with no occurrence comes back unchanged; overlapping and nested occurrences of the same or different words simply raise the cover count.

Time complexity:
O(n * S), where n = len(message) and S is the total length of the distinct banned words
Space complexity:
O(n + S)