问题描述

524. 通过删除字母匹配到字典里最长单词 (Medium)

给你一个字符串 s 和一个字符串数组 dictionary ,找出并返回 dictionary

中最长的字符串,该字符串可以通过删除 s 中的某些字符得到。

如果答案不止一个,返回长度最长且字母序最小的字符串。如果答案不存在,则返回空字符串。

示例 1:

输入:s = "abpcplea", dictionary =
["ale","apple","monkey","plea"]
输出:"apple"

示例 2:

输入:s = "abpcplea", dictionary = ["a","b","c"]
输出:"a"

提示:

  • 1 <= s.length <= 1000
  • 1 <= dictionary.length <= 1000
  • 1 <= dictionary[i].length <= 1000
  • sdictionary[i] 仅由小写英文字母组成

解题思路

首先将dictionary按长度从大到小排序,相同长度的字符串,字典序小的在前面;

判断dictionary中的字符串是否能通过删除s中的某些字符得到可以利用双指针优化时间复杂度为\(O(n)\),ns的长度。

代码

class Solution {
public:
bool IsSub(string &s, string &word) {
for (int i = 0, j = 0; j < word.size();) {
if (i == s.size()) {
return false;
}
if (s[i] == word[j]) {
i++;
j++;
} else {
i++;
}
}
return true;
}
string findLongestWord(string s, vector<string> &dictionary) {
auto cmp = [&](string &s1, string &s2) {
if (s1.size() != s2.size()) {
return s1.size() > s2.size();
}
return s1 < s2;
};
std::sort(dictionary.begin(), dictionary.end(), cmp);
for (auto &word : dictionary) {
if (IsSub(s, word)) {
return word;
}
}
string res;
return res;
}
};

524. 通过删除字母匹配到字典里最长单词 (Medium)的更多相关文章

  1. Java实现 LeetCode 524 通过删除字母匹配到字典里最长单词(又是一道语文题)

    524. 通过删除字母匹配到字典里最长单词 给定一个字符串和一个字符串字典,找到字典里面最长的字符串,该字符串可以通过删除给定字符串的某些字符来得到.如果答案不止一个,返回长度最长且字典顺序最小的字符 ...

  2. leetcode.双指针.524通过删除字母匹配到字典里最长单词-Java

    1. 具体题目 给定一个字符串和一个字符串字典,找到字典里面最长的字符串,该字符串可以通过删除给定字符串的某些字符来得到.如果答案不止一个,返回长度最长且字典顺序最小的字符串.如果答案不存在,则返回空 ...

  3. [Swift]LeetCode524. 通过删除字母匹配到字典里最长单词 | Longest Word in Dictionary through Deleting

    Given a string and a string dictionary, find the longest string in the dictionary that can be formed ...

  4. 【LeetCode】524-通过删除字母匹配到字典里最长单词

    题目描述 给定一个字符串和一个字符串字典,找到字典里面最长的字符串,该字符串可以通过删除给定字符串的某些字符来得到.如果答案不止一个,返回长度最长且字典顺序最小的字符串.如果答案不存在,则返回空字符串 ...

  5. leetcode 524. Longest Word in Dictionary through Deleting 通过删除字母匹配到字典里最长单词

    一.题目大意 https://leetcode.cn/problems/longest-word-in-dictionary-through-deleting 给你一个字符串 s 和一个字符串数组 d ...

  6. LeetCode 720. Longest Word in Dictionary (字典里最长的单词)

    Given a list of strings words representing an English Dictionary, find the longest word in words tha ...

  7. Word Break II 求把字符串拆分为字典里的单词的全部方案 @LeetCode

    这道题相似  Word Break 推断能否把字符串拆分为字典里的单词 @LeetCode 只不过要求计算的并不不过能否拆分,而是要求出全部的拆分方案. 因此用递归. 可是直接递归做会超时,原因是Le ...

  8. [LeetCode] Longest Word in Dictionary through Deleting 删除后得到的字典中的最长单词

    Given a string and a string dictionary, find the longest string in the dictionary that can be formed ...

  9. 原生Js汉语拼音首字母匹配城市名/自动提示列表

    根据城市的汉语名称首字母把城市排序,基本思路: 1.处理数据,按照需要的格式分别添加{HOT:{hot:[],ABCDEFG:{a:[1,2,3],b:[1,2,3]},HIGHLMN:{},OPQR ...

  10. 计蒜客 删除字母'c'

    删除字母'c' 右侧的程序实现的功能是从字符串s中删除所有的小写字母c,请改正程序错误的地方. 注意:main函数不可以改动,程序结构也不能修改. 很简单的哦,加油吧- 样例输入 abccabcn 样 ...

随机推荐

  1. ssm——spring整理

    目录 1.概述 2.Spring工厂与IOC 2.1.为什么要有Spring框架 2.2.什么是IOC 2.Spring工厂对实例注入 2.1.使用标签进行注入 2.2.使用注解进行注入 2.2.3. ...

  2. 基本能看懂的C编译器,只有365行!

    Fabrice Bellard is a French computer programmer known for writing FFmpeg, QEMU, and the Tiny C Compi ...

  3. python之路26 面向对象魔法方法、元类、元类定制类、对象的产生行为 __new__方法

    面向对象的魔法方法 魔法方法:类中定义的双下方法都称为魔法方法 不需要人为调用 在特定的条件下会自动触发运行 eg:__init__创建空对象之后自动触发给对象添加独有的数据 1.__init__ 对 ...

  4. SSM框架——整合ssm

    SSM整合 1.准备工作 新建一个普通的Maven项目 建好所有需要的架构层 向pom.xml中导入所有的依赖 <!--MyBatis相关--> <dependency> &l ...

  5. 【Redis实战专题】「性能监控系列」全方位探索Redis的性能监控以及优化指南

    Redis基本简介 Redis是一个开源(BSD 许可).内存存储的数据结构服务器,可用作数据库,高速缓存和消息队列代理.它支持字符串.哈希表.列表.集合.有序集合等数据类型.内置复制.Lua 脚本. ...

  6. 从0到1手把手教你实现vite系列--重写依赖请求路径,处理/@modules/vue引用

    前面以及写了三篇了,这是第四篇,等我写完就合并起来哦 这个是第一篇的链接:vite原理,创建项目,基础知识 这个是第二篇的链接Vite-中篇-通过服务访问静态资源以及重写请求路径 这个是第三篇的链接# ...

  7. Linux基础介绍

    Linux基础介绍 一.运维的本质 运维:运行维护应用程序 岗位需求:自动化运维.DBA.docker+K8s 运维的本质: 1.尽可能保证应用程序24小时不间断运行 2.尽可能保证数据的安全 3.尽 ...

  8. 洛谷p2669

    #include<bits/stdc++.h> using namespace std; int main() { int k,m=0,p=1;//p:给j个金币的第p天(1~j循环变化) ...

  9. Nginx06 Rewrite

    1 简介 rewrite模块即ngx_http_rewrite_module模块,主要功能是改写请求URI,是Nginx默认安装的模块.rewrite模块会根据PCRE正则匹配重写URI,然后发起内部 ...

  10. 【翻译】API 链接与键:为什么应该使用链接而不是键来表示 API 中的关系

    翻译自原文: https://cloud.google.com/blog/products/application-development/api-design-why-you-should-use- ...