题目链接

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. java四种访问权限修饰符

    java中四个访问权限修饰符: public(公开的).protected(受保护的).default(默认的).private(私有的). 它们决定了紧跟其后被定义的东西的使用范围. 适用范围< ...

  2. nodejs 中 module.exports 和 exports 的区别

    1. module应该是require方法中,上下文中的对象 2. exports对象应该是上下文中引用module.exports的新对象 3. exports.a = xxx 会将修改更新到mod ...

  3. NodeJS http模块

    Node.js提供了http模块,用于搭建HTTP服务端和客户端. 创建Web服务器 /** * node-http 服务端 */ let http = require('http'); let ur ...

  4. servlet 和 threadlocal 与 web容器(理解threadlocal)

    同步机制采用了“以时间换空间”的方式,提供一份变量,让不同的线程排队访问.而ThreadLocal采用了“以空间换时间”的方式,为每一个线程都提供了一份变量的副本,从而实现同时访问而互不影响. htt ...

  5. 20135234mqy 实验二 Java面向对象程序设计

      北京电子科技学院(BESTI) 实     验    报     告 课程:Java程序设计  班级:1352  姓名:mqy  学号:20135234 成绩:             指导教师: ...

  6. web04-LoginServlet

    电影网站:www.aikan66.com项目网站:www.aikan66.com游戏网站:www.aikan66.com图片网站:www.aikan66.com书籍网站:www.aikan66.com ...

  7. css3学习笔记三

    css3有些特殊的元素选择器这和jquery相似.效果图如下

  8. Java文件写入时是否覆盖

    这个是和服务器读数据结合着来的,是向服务器文件写数据,这就碰到了是否覆盖以前写的数据的问题,看FileWriter();的参数后面的参数名叫append,用词典查是附加的意思,灵机一动,改成false ...

  9. 配置JDBC

    在数据库和MyEclipse都安装好的情况下进行配置: 1.将JDBC(sqljdbc_4.0.2206.100_chs.exe)文件解压到C盘program files下面(也可以将解压后的文件Mi ...

  10. 项目Beta冲刺团队随笔集

    博客集如下: Beta冲刺Day1:第一天冲刺记录 Beta冲刺Day2:第二天冲刺记录 Beta冲刺Day3:第三天冲刺记录 Beta冲刺Day4:第四天冲刺记录 Beta冲刺Day5:第五天冲刺记 ...