FastPrepComment Word and Substring Search

Comment Word and Substring Search

Rippling logoRippling● MediumINTERNPHONE SCREEN
Learn

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 WORD mode, the query must equal a complete space-delimited word.
  • In SUBSTRING mode, 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 0 and 10^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.
  • mode is WORD or SUBSTRING.

More Rippling problems

See Rippling hiring insights
public int[] searchComments(int[] commentIds, String[] comments, String word, String mode) {
    // Write your code here.
}
commentIds[41,7,20]
comments["red bird","bluebird red","blue bird"]
word"bird"
mode"WORD"
expected[41,20]
Checking account…