2024-02-29|閱讀時間 ‧ 約 0 分鐘

系統實作題: 搜尋建議系統 Search Suggestions System_Leetcode 1268_精選75題

題目敘述

題目會給定兩個輸入。
第一個輸入是關鍵字清單products,第二個是使用者輸入的字串searchWord

要求我們實現關鍵字搜尋建議系統,使用者每輸入一個字元就推薦一次


推薦時,優先返回字典序(Lecial order)最接近的關鍵字,最多不要超過三個關鍵字


題目的原文敘述


測試範例

Example 1:

Input: 
products = ["mobile","mouse","moneypot","monitor","mousepad"], searchWord = "mouse"

Output:
[
["mobile","moneypot","monitor"], # 使用者輸入m
["mobile","moneypot","monitor"], # 使用者輸入mo
["mouse","mousepad"], # 使用者輸入mou
["mouse","mousepad"], # 使用者輸入mous
["mouse","mousepad"] # 使用者輸入mouse
]
Explanation: products sorted lexicographically = ["mobile","moneypot","monitor","mouse","mousepad"].
After typing m and mo all products match and we show user ["mobile","moneypot","monitor"].
After typing mou, mous and mouse the system suggests ["mouse","mousepad"].

Example 2:

Input: products = ["havana"], searchWord = "havana"
Output: [["havana"],["havana"],["havana"],["havana"],["havana"],["havana"]]
Explanation: The only word "havana" will be always suggested while typing the search word.

約束條件

Constraints:

  • 1 <= products.length <= 1000

關鍵字清單products 陣列長度介於1~1000。​

  • 1 <= products[i].length <= 3000

每個關鍵字長度介於1~3000。

  • 1 <= sum(products[i].length) <= 2 * 10^4

關鍵字總長度介於1~兩萬之間。

  • All the strings of products are unique.

所有關鍵字清單裡的關鍵字都是獨一無二的,不會重複

  • products[i] consists of lowercase English letters.

關鍵字都只會有小寫英文字母。

  • 1 <= searchWord.length <= 1000

使用者輸入的字串介於1~1000個字元之間。

  • searchWord consists of lowercase English letters.

使用者的輸入只會有小寫英文字母。

分享至
成為作者繼續創作的動力吧!
Leetcode 國際版精選75題 上機考面試題 詳解 裡面包含: 1. 內涵題意解析 2. 演算法建造 3. python解題程式碼 4. 複雜度分析 5. 關鍵知識點提示
從 Google News 追蹤更多 vocus 的最新精選內容從 Google News 追蹤更多 vocus 的最新精選內容

發表回應

成為會員 後即可發表留言