Comment Word and Substring Search
Problem statement
You are given parallel arrays commentIds and comments. Each comment has a unique integer ID and a text string. Return the IDs of comments matching the search word.
- In
WORDmode, the query must equal a complete space-delimited word. - In
SUBSTRINGmode, the query may occur inside a longer word.
Text and queries use lowercase English letters; text may also contain spaces. A word is a maximal nonempty run of letters. Return each matching ID once, in the same order as the input comments. The collection is fixed for this call.
Function
searchComments(commentIds: int[], comments: String[], word: String, mode: String) → int[]Examples
Example 1
commentIds = [41,7,20]comments = ["red bird","bluebird red","blue bird"]word = "bird"mode = "WORD"return = [41,20]Only complete tokens match; bluebird does not.
Example 2
commentIds = [41,7,20]comments = ["red bird","bluebird red","blue bird"]word = "bird"mode = "SUBSTRING"return = [41,7,20]The substring follow-up also matches bluebird.
Example 3
commentIds = [8,3]comments = ["green green","blue"]word = "green"mode = "WORD"return = [8]Repeated occurrences in one comment still produce one ID.
Constraints
0 <= commentIds.length = comments.length <= 1000.- Comment IDs are unique integers between
0and10^9. - The total number of text characters is at most
10^5. 1 <= word.length <= 100.- Texts contain lowercase English letters and spaces; the query contains lowercase English letters only.
modeisWORDorSUBSTRING.