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
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.
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
- Detection ignores case, while the output keeps the original characters. Compare against a lowercased view, then build the answer from message itself.
- A match may start inside another match, of the same word or a different one, and both still count.
- 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.