当前位置:网站首页>524.通过删除字母匹配到字典里最长单词
524.通过删除字母匹配到字典里最长单词
2022-07-30 05:38:00 【Linke66】


思路:
因为要找长度最长且字母序最小的字符串。
因此先对dictionary进行排序,按字符串长度降序,假如长度相同就按字母序,即默认的string排序即可。
接下来遍历dictionary每一个string元素,记为dic,看是否满足要求。
辅助函数,判断dic是否是s的子串:
假如dic长度大于s,那么必然不可能由s删除字符得到dic,返回false;
否则就用双指针的方法,判断即可,具体流程见代码。
class Solution {
public:
bool help(const string& s1,const string& s2)
{
if(s2.size()>s1.size()) return false; //s2不能比s1长
int i=0,j=0;
for(;i<s1.size();++i)
{
if(s1[i]==s2[j])
{
++j;
if(j==s2.size()) return true;
}
}
return false;
}
string findLongestWord(string s, vector<string>& dictionary) {
sort(dictionary.begin(),dictionary.end(),[](const string& a,const string& b)
{
return a.size()>b.size()||(a.size()==b.size()&&a<b);
});
for(auto &dic:dictionary)
{
if(help(s,dic)) return dic;
}
return string();
}
};
边栏推荐
- Learn FPGA from the underlying structure (6) ---- Distributed RAM (DRAM, Distributed RAM)
- 81.搜索旋转排序数组II(数组旋转后二分查找)
- list(列表)和array(数组)的区别
- np.argsort()函数详细解析
- [GO Language Basics] 1. Why do I want to learn Golang and the popularization of GO language entry
- Anaconda安装教程
- How is crawler data collected and organized?
- navicat连接MySQL报错:1045 - Access denied for user ‘root‘@‘localhost‘ (using password YES)
- net start mysql MySQL service is starting. MySQL service failed to start.The service did not report any errors.
- 【飞控开发基础教程9】疯壳·开源编队无人机-PWM(电机控制)
猜你喜欢

解决phpstudy无法启动MySQL服务

Programmers make money and practice, teach you how to do paid courses, self-media, paid articles and paid technical courses to make money

应用实践 | Apache Doris 在百度智能云计费账单系统的应用实践
![[GO语言基础] 一.为什么我要学习Golang以及GO语言入门普及](/img/ac/80ab67505f7df52d92a206bc3dd50e.png)
[GO语言基础] 一.为什么我要学习Golang以及GO语言入门普及

MySQL user authorization

navicat新建数据库

384.打乱数组

MySQL-Explain详解

解决没有配置本地nacos但是一直发生localhost8848连接异常的问题

cnpm安装步骤
随机推荐
破纪录者(Google Kickstart2020 Round D Problem A)
【Koltin Flow(二)】Flow操作符之末端操作符
从底层结构开始学习FPGA(6)----分布式RAM(DRAM,Distributed RAM)
navicat新建数据库
排列数字(DAY90)dfs
flask的笔记
np.argsort()函数详细解析
SOA(面向服务架构)是什么?
4、nerf(pytorch)
ClickHouse data insert, update and delete operations SQL
np.where()用法
[GLib] What is GType
图形镜像对称(示意图)
瑞吉外卖项目:新增菜品与菜品分页查询
php数组实现根据某个键值将相同键值合并生成新二维数组的方法
Detailed MySQL-Explain
MySQL 数据库基础知识(系统化一篇入门)
mysql 时间字段默认设置为当前时间
net start mysql MySQL service is starting. MySQL service failed to start.The service did not report any errors.
torch.optim.Adam()