← 返回 apple 的题目列表Design a Document Search Class for Word/Phrase Queries
类型:online_judge
Problem: Implement a Document Search Class (Word/Phrase Queries)
You are given a list of documents. Each document contains:
a unique integer id
a text field content
Implement a search class DocumentSearch that, for a given query query, returns all document ids that match.
Query Types
Single word: e.g. "apple", return all document ids whose content contains the word.
Multiple words (with spaces): e.g. "key words", return all document ids whose content contains this exact phrase consecutively (as a substring).
Required API
Implement a class DocumentSearch:
DocumentSearch(Document[] documents)
Initialize internal data structures using the given documents.
List<Integer> search(String query)
Return a list of all matching document ids.
The returned ids must be sorted in ascending order.
Notes
Document ids are unique.
Any reasonable data structure is allowed.
Example
Documents:
(id=1, content="apple banana")
(id=2, content="key words are important")
(id=3, content="a keyword is different from key words")
Queries:
search("apple") -> [1]
search("key words") -> [2, 3]
Example
Input
documents=[(1,"apple banana"),(2,"key words are important"),(3,"a keyword is different from key words")]
query="apple"
Output
[1]