An inverted index can tell whether terms occur in a document. A positional index also records where they occur, allowing the engine to distinguish an exact phrase from terms separated or reversed in the text.
Design a PositionalPhraseSearch class:
PositionalPhraseSearch() creates a stateless search helper.int[] search(String[] documents, String phrase) returns the indices of documents containing the complete phrase at consecutive word positions.
Lowercase documents and the phrase, then split them on single spaces. Positions are zero-based. A document may contain several phrase occurrences, but its index must appear only once. Return matching document indices in ascending order.
Example 1:
Input:
Output:
Explanation: Documents 0 and 2 contain consecutive terms. Document 1 has another word between them.
Example 2:
Input:
Output:
Explanation: Document 2 matches at positions 0 and 2, but the result contains its index only once.
Constraints
1 <= documents.length <= 10^41 <= documents[i].length, phrase.length <= 10^4- Text contains letters and single spaces, without leading or trailing spaces.
- The total number of document words is at most
10^5. - At most
100 calls are made to search.