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. css清除浮动float的几种方法

    摘要: css清除浮动float的三种方法总结,为什么清浮动?浮动会有那些影响?     一.抛一块问题砖(display: block)先看现象: 这里我没有给最外层的DIV.outer 设置高度, ...

  2. mvp和mvc的区别

    一句话总结:你代码逻辑有没有写在View中的,有就是MVC,没有就是MVP MVP模式: View不直接与Model交互,而是通过与Presenter交互来与Model间接交互 Presenter与V ...

  3. Unity3D事件顺序与功能

    Unity3D中所有控制脚本的基类MonoBehaviour有一些虚函数用于绘制中事件的回调,也可以直接理解为事件函数,例如大家都很清楚的Start,Update等函数,以下做个总结. Awake 当 ...

  4. 【转】Hibernate 原汁原味的四种抓取策略

    最近在研究 Hibernate 的性能优化的时候碰到了"抓取策略", 由于以前没有详细的研究过, 所以到处找资料, 但是无论从一些讲 Hibernate 书籍,还是他人 Blog ...

  5. JS兼容性汇总

    1.  Frame  (1)问题:在IE中可以用window.top.frameId和window.top.frameName来得到该Frame所代表的Window,Firefox中只能用window ...

  6. 剑指offer--30.二叉搜索树的后序遍历序列

    正常情况下,因为二叉搜索树,左子树所有结点比根小,右子树所有结点比根大,所以循环一遍就能结束 ----------------------------------------------------- ...

  7. jquery 中多条件选择器,相对选择器,层次选择器的区别

    一.Jquery常用的过滤选择器如下所示: 1.:first,选取第一个元素,比如$("div:first")选取第一个div元素 2.:last,选取最后一个元素,比如$(&qu ...

  8. Electron 使用 Webpack2 打包多入口应用程序

    Electron 使用 Webpack2 打包多入口应用程序 接前面一篇文章,前一篇文章中只有一个页面,并且只有一个js文件,所以打包的时候会把那个js打包成一个bundle.js文件.但是假如我们有 ...

  9. 服务器与客户端数据交互 (json)

    服务器返回到客户端json对象,是什么类型,而用ajax处理后,变成什么了.   > 猜测1:服务器返回的数据带双引号: "返回值",理由:因为返回的类型都是字符串. 结果无 ...

  10. LINUX系统yum安装SVN服务及其配置

    待: http://oplinux.com/app/svn/linux-yum-install-svn.html  //基础设置及流程 http://files.cnblogs.com/logon/s ...