1268:搜索推荐系统

This commit is contained in:
huangge1199 2021-09-06 17:00:27 +08:00
parent 8ae6bb5e4f
commit bfa9fe0e86
2 changed files with 205 additions and 0 deletions

View File

@ -0,0 +1,152 @@
//给你一个产品数组 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 中所有字符都是小写英文字母
//
// Related Topics 字典树 数组 字符串 👍 84 👎 0
package leetcode.editor.cn;
import java.util.*;
//1268:搜索推荐系统
class SearchSuggestionsSystem {
public static void main(String[] args) {
//测试代码
Solution solution = new SearchSuggestionsSystem().new Solution();
solution.suggestedProducts(new String[]{"mobile", "mouse", "moneypot", "monitor", "mousepad"}, "mouspepad");
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
List<String> getL;
public List<List<String>> suggestedProducts(String[] products, String searchWord) {
Arrays.sort(products);
Map<Character, Node> map = new HashMap<>();
for (String product : products) {
char ch = product.charAt(0);
Node root = map.getOrDefault(ch, new Node(ch));
Node temp = root;
for (int j = 1; j < product.length(); j++) {
if (temp.child[product.charAt(j) - 'a'] == null) {
temp.child[product.charAt(j) - 'a'] = new Node(product.charAt(j));
}
temp = temp.child[product.charAt(j) - 'a'];
if (j == product.length() - 1) {
temp.isEnd = true;
temp.str = product;
}
}
map.put(ch, root);
}
List<List<String>> result = new ArrayList<>();
Node node;
if (!map.containsKey(searchWord.charAt(0))) {
for (int i = 0; i < searchWord.length(); i++) {
result.add(new ArrayList<>());
}
return result;
}
node = map.get(searchWord.charAt(0));
getL = new ArrayList<>();
getStr(node);
result.add(new ArrayList<>(getL));
for (int i = 1; i < searchWord.length(); i++) {
char ch = searchWord.charAt(i);
getL = new ArrayList<>();
if (node.child[ch - 'a'] == null) {
break;
}
getStr(node.child[ch - 'a']);
node = node.child[ch - 'a'];
result.add(new ArrayList<>(getL));
}
int size = result.size();
for (int i = 0; i < searchWord.length() - size; i++) {
result.add(new ArrayList<>());
}
return result;
}
public void getStr(Node root) {
if (root.isEnd) {
getL.add(root.str);
}
for (int i = 0; i < 26; i++) {
if (getL.size() == 3) {
break;
}
if (root.child[i] != null) {
getStr(root.child[i]);
}
}
}
class Node {
char ch;
boolean isEnd;
Node[] child;
String str;
public Node(char ch) {
this.ch = ch;
child = new Node[26];
isEnd = false;
}
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,53 @@
<p>给你一个产品数组&nbsp;<code>products</code>&nbsp;和一个字符串&nbsp;<code>searchWord</code>&nbsp;<code>products</code>&nbsp; 数组中每个产品都是一个字符串。</p>
<p>请你设计一个推荐系统,在依次输入单词&nbsp;<code>searchWord</code> 的每一个字母后,推荐&nbsp;<code>products</code> 数组中前缀与&nbsp;<code>searchWord</code> 相同的最多三个产品。如果前缀相同的可推荐产品超过三个,请按字典序返回最小的三个。</p>
<p>请你以二维列表的形式,返回在输入&nbsp;<code>searchWord</code>&nbsp;每个字母后相应的推荐产品的列表。</p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre><strong>输入:</strong>products = [&quot;mobile&quot;,&quot;mouse&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mousepad&quot;], searchWord = &quot;mouse&quot;
<strong>输出:</strong>[
[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],
[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],
[&quot;mouse&quot;,&quot;mousepad&quot;],
[&quot;mouse&quot;,&quot;mousepad&quot;],
[&quot;mouse&quot;,&quot;mousepad&quot;]
]
<strong>解释:</strong>按字典序排序后的产品列表是 [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mouse&quot;,&quot;mousepad&quot;]
输入 m 和 mo由于所有产品的前缀都相同所以系统返回字典序最小的三个产品 [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;]
输入 mou mous 和 mouse 后系统都返回 [&quot;mouse&quot;,&quot;mousepad&quot;]
</pre>
<p><strong>示例 2</strong></p>
<pre><strong>输入:</strong>products = [&quot;havana&quot;], searchWord = &quot;havana&quot;
<strong>输出:</strong>[[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;]]
</pre>
<p><strong>示例 3</strong></p>
<pre><strong>输入:</strong>products = [&quot;bags&quot;,&quot;baggage&quot;,&quot;banner&quot;,&quot;box&quot;,&quot;cloths&quot;], searchWord = &quot;bags&quot;
<strong>输出:</strong>[[&quot;baggage&quot;,&quot;bags&quot;,&quot;banner&quot;],[&quot;baggage&quot;,&quot;bags&quot;,&quot;banner&quot;],[&quot;baggage&quot;,&quot;bags&quot;],[&quot;bags&quot;]]
</pre>
<p><strong>示例 4</strong></p>
<pre><strong>输入:</strong>products = [&quot;havana&quot;], searchWord = &quot;tatiana&quot;
<strong>输出:</strong>[[],[],[],[],[],[],[]]
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= products.length &lt;= 1000</code></li>
<li><code>1 &lt;= &Sigma; products[i].length &lt;= 2 * 10^4</code></li>
<li><code>products[i]</code>&nbsp;中所有的字符都是小写英文字母。</li>
<li><code>1 &lt;= searchWord.length &lt;= 1000</code></li>
<li><code>searchWord</code>&nbsp;中所有字符都是小写英文字母。</li>
</ul>
<div><div>Related Topics</div><div><li>字典树</li><li>数组</li><li>字符串</li></div></div><br><div><li>👍 84</li><li>👎 0</li></div>