A full-text engine maps terms to posting lists instead of scanning every document for every query term. Each posting can carry a term frequency, which becomes a simple relevance score.
Design an InvertedIndexSearch class:
InvertedIndexSearch() creates a stateless search helper.int[] search(String[] documents, String query) returns matching document indices in rank order.
Lowercase and split documents and the query on single spaces. Build an inverted index from term to (documentIndex, frequency) postings. A document's score is the total frequency of all query terms; repeated terms in the query contribute repeatedly. Return only positive-score documents, sorted by score descending and then document index ascending.
Example 1:
Input:
Output:
Explanation: Document 2 scores 3, document 0 scores 2, and document 1 does not match.
Example 2:
Input:
Output:
Explanation: Document 1 contains banana twice and ranks above document 0, which contains it once.
Constraints
1 <= documents.length <= 10^41 <= documents[i].length, query.length <= 10^4- Documents and query contain letters and single spaces, with no leading or trailing space.
- The total number of document words is at most
10^5. - At most
100 calls are made to search.