public class Solution {
public string FindLongestWord(string s, IList<string> d) {
string longest = "";
foreach (var dictWord in d)
{
int i = ;
foreach (var c in s)
{
if (i < dictWord.Length && c == dictWord[i])
{
i++;
}
} if (i == dictWord.Length && dictWord.Length >= longest.Length)
{
if (dictWord.Length > longest.Length || dictWord.CompareTo(longest) < )
{
longest = dictWord;
}
}
}
return longest;
}
}

https://leetcode.com/problems/longest-word-in-dictionary-through-deleting/#/description

补充一个python的实现:

 class Solution:
def findLongestWord(self, s: str, d: 'List[str]') -> str:
maxlen = 0
longestword = ''
for sd in d:
curlen = 0
i=0
j=0
while i<len(s) and j<len(sd):
if s[i]==sd[j]:
curlen += 1
if curlen == len(sd):
if curlen > maxlen or (curlen == maxlen and sd<longestword):
maxlen = curlen
longestword = sd break
else:
i+=1
j+=1
else:
i+=1
return longestword

再补充一个java实现:

 class Solution {
public String findLongestWord(String s, List<String> d) {
String longestWord = "";
for (String target : d) {
int l1 = longestWord.length(), l2 = target.length();
if (l1 > l2 || (l1 == l2 && longestWord.compareTo(target) < )) {
continue;
}
if (isSubstr(s, target)) {
longestWord = target;
}
}
return longestWord;
} private boolean isSubstr(String s, String target) {
int i = , j = ;
while (i < s.length() && j < target.length()) {
if (s.charAt(i) == target.charAt(j)) {
j++;
}
i++;
}
return j == target.length();
}
}

leetcode524的更多相关文章

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

随机推荐

  1. 【51nod-1605】棋盘问题

    上帝创造了一个n*m棋盘,每一个格子都只有可能是黑色或者白色的. 亚当和夏娃在玩一个游戏,每次寻找边长为x的正方形,其中每个格子必须为黑色,然后将这些格子染白. 如果谁不能操作了,那么那个人就输了. ...

  2. ubuntu 交叉编译arm linux 内核小例子

    下载arm-linux-gcc 4.2.3http://code.google.com/p/princess-alist/downloads/detail?name=arm-linux-gcc-4.3 ...

  3. 通过TortoiseSVN checkout的文件前面没有“状态标识”

    问题描述:安装完成VisualSVN Server.VisualSVn和TortoiseSVN后,然后通过SVN Server新建Repository(仓库),用Visual Studio新建一个So ...

  4. C#中upd分包与发送,已经实现全部代码

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Tool ...

  5. gethostbyname()函数

    gethostbyname()函数说明——用域名或主机名获取IP地址 包含头文件    #include <netdb.h>    #include <sys/socket.h> ...

  6. Arcgis for Js实现graphiclayer的空间查询(续)

    上文中,实现了简单的针对graphiclayer的空间查询工作,在本节,将更加详细的介绍针对graphiclayer的空间查询.首先,空间查询的方式:提供多种类型的空间查询,包括点周边.线周边.面内等 ...

  7. 使用tor实现匿名扫描/SSH登录

    你要做坏事时,最先应该想到匿名.扫描网站/主机,或利用漏洞:甚至在大天朝发帖都有风险,为了防止半夜鬼敲门,我们可以使用tor实现匿名. 如果你不知道tor是什么,看:https://zh.wikipe ...

  8. NOIP2013DAY1题解

    T1转圈游戏 十月のsecret 题解:快速幂 代码: #include<iostream> #include<cstring> #include<cstdio> ...

  9. openfaas 简单试用

    1. 安装 faas-cli  参考以前文章,或者使用官方的shell脚本   2. 简单例子 mkdir rong cd rong faas-cli new rong --lang python / ...

  10. Javascript 原型链资料收集

    Javascript 原型链资料收集 先收集,后理解. 理解JavaScript的原型链和继承 https://blog.oyanglul.us/javascript/understand-proto ...