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. LeetCode OJ:Maximum Product Subarray(子数组最大乘积)

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  2. String、StringBuffer、StringBuilder分析(jdk8)

    以下代码只挑选了关键方法进行分析 public final class String //String类不可继承,实现了序列化 implements java.io.Serializable, Com ...

  3. NSPredicate(正则表达式)

    1. 正则表达式使用单个字符串来描述.匹配一系列符合某个句法规则的字符串.通常被用来检索.替换那些符合某个模式的文本. 2. iOS中正则使用 有三种(NSPredicate, rangeOfStri ...

  4. Python基础学习(第2天)

    第三课:序列(sequence) 1.序列是一种有顺序的元素的集合 序列可以包含1个或多个元素,也可以不包括任何元素: 序列中的元素可以是[基础数据类型]中任一种,也可以是[别的序列]. s1 = ( ...

  5. Activity Process Task Application 专题讲解

    Activity Process Task Application 专题讲解 Activity.和进程 为了阅读方便,将文档转成pdf http://files.cnblogs.com/franksu ...

  6. Http权威指南(概述篇总结)

    之前的<锋利的jQuery>后面陆续翻完了,实在觉得没什么值得记录的,也就没继续写了,然后看见书架上有 本去年买的<Http权威指南>,其实做web编程的,对于Http协议还是 ...

  7. MbrFix 问题

    删除Windows/Linux双系统下的Linux系统 参考博客 注意:官网上的 MbrFix.exe 下载或许太慢. CSDN下载 问题 1.看到博客下面的评论写道: 为什么我的到第四步的时候会提示 ...

  8. (一)canvas简介

    <canvas>元素主要用来图形的绘制,通过脚本来完成(通常时js来实现): 可以利用其实现图表,游戏等项目的开发. getContext 获取画布的摸板是2d还是3d strokeRec ...

  9. volatile 续

    上次的问题在看了一篇博客后有了点理解了 博文地址为 http://www.cnblogs.com/dolphin0520/p/3920373.html 按照文章中写的,在并发编程中,我们通常会遇到以下 ...

  10. HDU3507Print Article (斜率优化DP)

    Zero has an old printer that doesn't work well sometimes. As it is antique, he still like to use it ...