地址 https://leetcode-cn.com/problems/search-suggestions-system/

题目描述
给你一个产品数组 products 和一个字符串 searchWord ,products  数组中每个产品都是一个字符串。

请你设计一个推荐系统,在依次输入单词 searchWord 的每一个字母后,推荐 products 数组中前缀与 searchWord 相同的最多三个产品。如果前缀相同的可推荐产品超过三个,请按字典序返回最小的三个。

请你以二维列表的形式,返回在输入 searchWord 每个字母后相应的推荐产品的列表。

样例

示例 :

输入: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"]
示例 : 输入:products = ["havana"], searchWord = "havana"
输出:[["havana"],["havana"],["havana"],["havana"],["havana"],["havana"]]
示例 : 输入:products = ["bags","baggage","banner","box","cloths"], searchWord = "bags"
输出:[["baggage","bags","banner"],["baggage","bags","banner"],["baggage","bags"],["bags"]]
示例 : 输入:products = ["havana"], searchWord = "tatiana"
输出:[[],[],[],[],[],[],[]]
  提示: <= products.length <=
<= Σ products[i].length <= * ^
products[i] 中所有的字符都是小写英文字母。
<= searchWord.length <=
searchWord 中所有字符都是小写英文字母。

算法1
据大佬们说 有什么前缀树的解答
我想了想用trie树的方案 没结果
就开始硬怼了。
首先在products 按照字典序排序,然后另开数组记录对应的单词 与 搜索单词相同的字母数

再遍历这个记录相同字母数的数组 找到相同一个单字的词汇 找到相同两个单字的词汇。。。。。。
遍历或者找到或者找不到为空, 中途剪枝为如果找到符合要求的3个词汇也提前退出(由于已经按照字典序排序,最开始找到的符合要求的3个词汇肯定是字典序优先的)

C++ 代码

class Solution {
public:
vector<vector<string>> ret; int GetsameCharCount(const string& target,const string& src){
int ret =;
for(int i=;i < target.size() && i < src.size();i++ ){
if(target[i] == src[i]) ret++;
else{
break;
}
}
return ret;
} vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord) {
sort(products.begin(),products.end());
vector<int> sameCharCount;
for(int i = ; i < products.size();i++){
int t = GetsameCharCount(products[i],searchWord);
sameCharCount.push_back(t);
} for(int i = ; i < searchWord.size();i++){
vector<string> v;
for(int j = ; j < sameCharCount.size();j++){
if(sameCharCount[j] >i){
v.push_back(products[j]);
}
if(v.size() == ) break;
}
ret.push_back(v);
} return ret;
}
};

LeetCode 5273. 搜索推荐系统 Search Suggestions System的更多相关文章

  1. 【leetcode】1268. Search Suggestions System

    题目如下: Given an array of strings products and a string searchWord. We want to design a system that su ...

  2. [LeetCode] Design Search Autocomplete System 设计搜索自动补全系统

    Design a search autocomplete system for a search engine. Users may input a sentence (at least one wo ...

  3. [LeetCode] 642. Design Search Autocomplete System 设计搜索自动补全系统

    Design a search autocomplete system for a search engine. Users may input a sentence (at least one wo ...

  4. [LeetCode] 95. Unique Binary Search Trees II(给定一个数字n,返回所有二叉搜索树) ☆☆☆

    Unique Binary Search Trees II leetcode java [LeetCode]Unique Binary Search Trees II 异构二叉查找树II Unique ...

  5. LeetCode 81——搜索旋转排序数组 II

    1. 题目 2. 解答 2.1. 方法一 基于 LeetCode 33--搜索旋转排序数组 中的方法二. 当 nums[mid] = nums[right] 时,比如 [1, 1, 2, 1, 1], ...

  6. LeetCode:搜索旋转排序数组【33】

    LeetCode:搜索旋转排序数组[33] 题目描述 假设按照升序排序的数组在预先未知的某个点上进行了旋转. ( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2]  ...

  7. Design Search Autocomplete System

    Design a search autocomplete system for a search engine. Users may input a sentence (at least one wo ...

  8. 集束搜索beam search和贪心搜索greedy search

    贪心搜索(greedy search) 贪心搜索最为简单,直接选择每个输出的最大概率,直到出现终结符或最大句子长度. 集束搜索(beam search) 集束搜索可以认为是维特比算法的贪心形式,在维特 ...

  9. [leetcode]95. Unique Binary Search Trees II给定节点形成不同BST的集合

    Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1 ...

随机推荐

  1. Python中通过csv的writerow输出的内容有多余的空行两种方法

    第一种方法 如下生成的csv文件会有多个空行 import csv #python2可以用file替代open with open("test.csv","w" ...

  2. 【产品】移动应用监控平台调研——bugly&fabric

    产品定位 腾讯bugly和fabric不仅仅是可以帮助运营人员分析用户.优化推广的数据分析平台,也是移动开发者的异常上报平台和应用更新平台.可以同时为公司产品运营和开发人员提供服务. 产品功能 fab ...

  3. [css flex布局]实例一,本来还想挺简单的,弄了挺久呢,先写一部分

    全是代码,直接拷走吧,看是不怎么好看的 参考:http://www.ruanyifeng.com/blog/search.html?cx=016304377626642577906%3Ab_e9ska ...

  4. zhy2_rehat6_mysql04 - MHA_故障演示与切换.txt

    export LANG=en_US 环境:------------------------------------------ 机器 VPN ip linux 账号/密码manager1 172.28 ...

  5. Spring 中的观察者模式

    一.Spring 中观察者模式的四个角色 1. 事件(ApplicationEvent) ApplicationEvent 是所有事件对象的父类.ApplicationEvent 继承自 jdk 的 ...

  6. Codeforces 1238 D. AB-string

    思维题 这次cf思维题好多啊 定义了good string 指一个串,其中每一个字符都属于一个长度>=2 的回文串的一部分.叫你求一个串中有几个good substring. 显然ab串 goo ...

  7. SpringBoot源码学习系列之@PropertySource不支持yaml读取原因

    然后,为什么@PropertySource注解默认不支持?可以简单跟一下源码 @PropertySource源码: 根据注释,默认使用DefaultPropertySourceFactory类作为资源 ...

  8. 超级简单的数组加单链表实现Map

    /** * 超级简单的数组加单链表实现Map * @author jlj * */ public class MyHashMap { public MyList[] lists; public int ...

  9. 弹指间,网页灰飞烟灭——Google灭霸彩蛋实现

    不知道大家有没有看这段时间最火的一部电影<复仇者联盟4:终局之战>,作为漫威迷的我还没看,为什么呢?因为太贵了,刚上映的那周,一张IMAX厅的票价已经达到了299的天价,作为搬砖民工是舍不 ...

  10. Java题库——Chapter6 一维数组

    1)What is the representation of the third element in an array called a? 1) _______ A)a(3) B) a(2) C) ...