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

2024/02/29閱讀時間約 7 分鐘

題目敘述

題目會給定兩個輸入。
第一個輸入是關鍵字清單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.

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

以行動支持創作者!付費即可解鎖
本篇內容共 3172 字、0 則留言,僅發佈於Leetcode 精選75題 上機考面試題 詳解你目前無法檢視以下內容,可能因為尚未登入,或沒有該房間的查看權限。
39會員
277內容數
由有業界實戰經驗的演算法工程師, 手把手教你建立解題的框架, 一步步寫出高效、清晰易懂的解題答案。 著重在讓讀者啟發思考、理解演算法,熟悉常見的演算法模板。 深入淺出地介紹題目背後所使用的演算法意義,融會貫通演算法與資料結構的應用。 在幾個經典的題目融入一道題目的多種解法,或者同一招解不同的題目,擴展廣度,並加深印象。
留言0
查看全部
發表第一個留言支持創作者!