Problem · Array
Sequential Phrase Search Across Documents
Learn this problemProblem statement
Given an array of text documents and a query phrase, return the zero-based indices of all documents that contain the phrase.
A phrase matches when all of its words appear as one contiguous sequence of complete words in the same order. Words are lowercase English-letter tokens separated by one space. Include each matching document index once, in increasing order.
Function
findDocumentsWithPhrase(documents: String[], phrase: String) → int[]Examples
Example 1
documents = ["distributed systems need careful testing","careful distributed systems testing","distributed reliable systems"]phrase = "distributed systems"return = [0,1]The first two documents contain distributed systems contiguously. The third separates the two words with reliable.
Example 2
documents = ["alpha beta gamma","beta alpha gamma","alpha gamma beta"]phrase = "alpha beta"return = [0]Only document 0 contains the two query words next to each other in the requested order.
Constraints
1 <= documents.length <= 1000.1 <= documents[i].length <= 1000.1 <= phrase.length <= 200.- Every document and
phrasecontains lowercase English words separated by exactly one space, with no leading or trailing space.