题目链接

Longest Word in Dictionary through Deleting - LeetCode

注意点

  • 长度一样的字符串要按字典序返回较小的

解法

解法一:遍历字典中的单词,用一个变量i来记录单词中的某个字母的位置,我们遍历给定字符串,如果遍历到单词中的某个字母来,i自增1,如果没有,就继续往下遍历。这样如果最后i和单词长度相等,说明单词中的所有字母都按顺序出现在了字符串s中。如果能得到,而且单词长度大于等于结果ret的长度,我们再看是否需要更新结果ret,有两种情况是必须要更新结果ret的,一个是当前单词长度大于结果ret的长度,另一种是当前单词长度和ret相同,但是字母顺序小于结果ret,这两种情况下更新结果ret即可

class Solution {
public:
string findLongestWord(string s, vector<string>& d) {
string ret = "";
int i,m,n = d.size();
for(i = 0;i < n;i++)
{
int j = 0;
m = d[i].size();
for(char c:s)
{
if(j < m && c == d[i][j]) j++;
}
if(j == m)
{
if(m > ret.size()) ret = d[i];
else if(m == ret.size() && ret > d[i]) ret = d[i];
}
}
return ret;
}
};

小结

  • 一开始没看懂题目是什么意思,理解成最长前缀了,其实题目是问能否将s中的某些字母删除而得到字典中的单词

Longest Word in Dictionary through Deleting - LeetCode的更多相关文章

  1. 【LeetCode】Longest Word in Dictionary through Deleting 解题报告

    [LeetCode]Longest Word in Dictionary through Deleting 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode. ...

  2. [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 ...

  3. #Leetcode# 524. Longest Word in Dictionary through Deleting

    https://leetcode.com/problems/longest-word-in-dictionary-through-deleting/ Given a string and a stri ...

  4. LeetCode——Longest Word in Dictionary through Deleting

    1. Question Given a string and a string dictionary, find the longest string in the dictionary that c ...

  5. [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 ...

  6. 524. Longest Word in Dictionary through Deleting【Medium】【删除后得到的字典中的最长单词】

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

  7. 524. Longest Word in Dictionary through Deleting

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

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

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

  9. [LeetCode] Longest Word in Dictionary 字典中的最长单词

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

随机推荐

  1. jQuery中turn.js(翻页效果)学习笔记

    Turn.js是一个内置的jQuery翻页插件1 html中引入<script type="text/javascript" src="js/turn.js&quo ...

  2. 会了这十种Python优雅的写法,让你工作效率翻十倍,一人顶十人用!

      我们都知道,Python 的设计哲学是「优雅」.「明确」.「简单」.这也许很多人选择 Python 的原因.但是我收到有些伙伴反馈,他写的 Python 并不优雅,甚至很臃肿,那可能是你的姿势不对 ...

  3. kubernetes高可用设计-CA,etcd

    环境准备: master01:192.168.150.128 master02:192.168.150.130 master03:192.168.150.131 node01:192.168.150. ...

  4. 简介make命令和makefile文件

    一.为什么要用到 make 命令和 makefile 文件 在 Linux 下编写一个程序,每次编译都需要在命令行一行一行的敲命令.如果是一个很小的程序还好说,命令不怎的复杂,编译速度也挺快,但是对于 ...

  5. Paxos共识算法

    Paxos共识算法 paxos是一族用来解决分布式系统共识的基础算法,共识过程就是在一组节点上达成一个一致的结果.由于节点可能会错误,通讯消息也可能会丢失,所以建立共识是一个比较复杂的过程. paxo ...

  6. 笨办法学Python - 习题3: Numbers and Math

    目录 习题 3: 数字和数学计算 算术运算符 加分习题: 我的答案: 总结: 扩展: Python比较运算符 Python赋值运算符 Python位运算符 Python逻辑运算符 Python成员运算 ...

  7. [salt] jinja模板中变量使用pillar的几种方法

    先转载下jinja模板中使用变量的方法,后文主要讲解pillar的变量使用方法 一.jinja模版的使用方法: 1.file状态使用template参数 - template:jinja 2.模版文件 ...

  8. Daily Scrumming* 2015.10.30(Day 11)

    一.总体情况总结 今日项目总结: 1.前后端同一了API设计以及API权限认证.用户状态保存的开发方案 2.API以及后端模型已经开始开发,前端UEditor开始学习,本周任务有良好的起步 3.前后端 ...

  9. IIs8 svc

    IIS8中添加WCF支持几种方法小结[图文] 方法一 最近在做Silverlight,Windows Phone应用移植到Windows 8平台,在IIS8中测试一些传统WCF服务应用,发现IIS8不 ...

  10. java语言复制数组的四种方法

    JAVA语言的下面几种数组复制方法中,哪个效率最高? B.效率:System.arraycopy > clone > Arrays.copyOf > for循环 1.System.a ...