Streaming Keyword Redaction
Problem statement
Given text and unique case-sensitive keywords, replace every keyword that appears as a complete word with the same number of * characters.
A word is a maximal run of ASCII letters, digits, or underscores. Preserve every non-word character exactly. Substrings inside a larger word are not replaced.
Process the text in one left-to-right pass so the method can be adapted to a very large file.
Function
redactKeywords(text: String, keywords: String[]) → StringExamples
Example 1
text = "red blue redwood"keywords = ["red"]return = "*** blue redwood"The standalone red is replaced, while the prefix inside redwood is preserved.
Example 2
text = "User_1,user_1!"keywords = ["user_1"]return = "User_1,******!"Matching is case-sensitive and underscore is part of a word.
Example 3
text = ""keywords = ["secret"]return = ""Empty text remains empty.
Constraints
0 <= text.length <= 1000000.0 <= keywords.length <= 100000.- Each keyword is a nonempty ASCII word with length at most
100. - The total keyword length is at most
1000000.