给你一个产品数组 products
和一个字符串 searchWord
,products
数组中每个产品都是一个字符串。
请你设计一个推荐系统,在依次输入单词 searchWord
的每一个字母后,推荐 products
数组中前缀与 searchWord
相同的最多三个产品。如果前缀相同的可推荐产品超过三个,请按字典序返回最小的三个。
请你以二维列表的形式,返回在输入 searchWord
每个字母后相应的推荐产品的列表。
示例 1:
输入:products = ["mobile","mouse","moneypot","monitor","mousepad"], searchWord = "mouse" 输出:[ ["mobile","moneypot","monitor"], ["mobile","moneypot","monitor"], ["mouse","mousepad"], ["mouse","mousepad"], ["mouse","mousepad"] ] 解释:按字典序排序后的产品列表是 ["mobile","moneypot","monitor","mouse","mousepad"] 输入 m 和 mo,由于所有产品的前缀都相同,所以系统返回字典序最小的三个产品 ["mobile","moneypot","monitor"] 输入 mou, mous 和 mouse 后系统都返回 ["mouse","mousepad"]
示例 2:
输入:products = ["havana"], searchWord = "havana" 输出:[["havana"],["havana"],["havana"],["havana"],["havana"],["havana"]]
示例 3:
输入:products = ["bags","baggage","banner","box","cloths"], searchWord = "bags" 输出:[["baggage","bags","banner"],["baggage","bags","banner"],["baggage","bags"],["bags"]]
示例 4:
输入:products = ["havana"], searchWord = "tatiana" 输出:[[],[],[],[],[],[],[]]
提示:
1 <= products.length <= 1000
1 <= Σ products[i].length <= 2 * 10^4
products[i]
中所有的字符都是小写英文字母。1 <= searchWord.length <= 1000
searchWord
中所有字符都是小写英文字母。
方法一:排序 + 前缀树
class Trie:
def __init__(self):
self.children = [None] * 26
self.v = []
def insert(self, word, i):
node = self
for c in word:
idx = ord(c) - ord('a')
if node.children[idx] is None:
node.children[idx] = Trie()
node = node.children[idx]
node.v.append(i)
def search(self, word):
res = [[] for _ in range(len(word))]
node = self
for i, c in enumerate(word):
idx = ord(c) - ord('a')
if node.children[idx] is None:
break
node = node.children[idx]
res[i] = node.v[:3]
return res
class Solution:
def suggestedProducts(
self, products: List[str], searchWord: str
) -> List[List[str]]:
products.sort()
trie = Trie()
for i, w in enumerate(products):
trie.insert(w, i)
res = trie.search(searchWord)
return [[products[j] for j in v] for v in res]
class Trie {
Trie[] children = new Trie[26];
List<Integer> v = new ArrayList<>();
void insert(String word, int i) {
Trie node = this;
for (char c : word.toCharArray()) {
c -= 'a';
if (node.children[c] == null) {
node.children[c] = new Trie();
}
node = node.children[c];
if (node.v.size() < 3) {
node.v.add(i);
}
}
}
List<List<Integer>> search(String word) {
List<List<Integer>> res = new ArrayList<>();
int n = word.length();
for (int i = 0; i < n; ++i) {
res.add(new ArrayList<>());
}
Trie node = this;
for (int i = 0; i < n; ++i) {
char c = word.charAt(i);
c -= 'a';
if (node.children[c] == null) {
break;
}
node = node.children[c];
res.set(i, node.v);
}
return res;
}
}
class Solution {
public List<List<String>> suggestedProducts(String[] products, String searchWord) {
Arrays.sort(products);
Trie trie = new Trie();
for (int i = 0; i < products.length; ++i) {
trie.insert(products[i], i);
}
List<List<Integer>> res = trie.search(searchWord);
List<List<String>> ans = new ArrayList<>();
for (List<Integer> v : res) {
List<String> t = new ArrayList<>();
for (int i : v) {
t.add(products[i]);
}
ans.add(t);
}
return ans;
}
}
type Trie struct {
children [26]*Trie
v []int
}
func newTrie() *Trie {
return &Trie{}
}
func (this *Trie) insert(word string, i int) {
node := this
for _, c := range word {
c -= 'a'
if node.children[c] == nil {
node.children[c] = newTrie()
}
node = node.children[c]
if len(node.v) < 3 {
node.v = append(node.v, i)
}
}
}
func (this *Trie) search(word string) [][]int {
node := this
n := len(word)
res := make([][]int, n)
for i, c := range word {
c -= 'a'
if node.children[c] == nil {
break
}
node = node.children[c]
res[i] = node.v
}
return res
}
func suggestedProducts(products []string, searchWord string) [][]string {
sort.Strings(products)
trie := newTrie()
for i, w := range products {
trie.insert(w, i)
}
res := trie.search(searchWord)
var ans [][]string
for _, v := range res {
t := []string{}
for _, i := range v {
t = append(t, products[i])
}
ans = append(ans, t)
}
return ans
}