leetcode524
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的更多相关文章
- [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 ...
随机推荐
- python:一个比较有趣的脚本
宿舍火星wifi经常掉,然后要重启,于是用Python写了一个脚本,用来远程控制火星wifi的重启 思路: 01.使用socket通讯 02.在wifi主机(开wifi的电脑)上运行客户端,控制机运行 ...
- 【JavsScript高级程序设计】学习笔记[1]
1. 在HTML中使用javascript 在使用script嵌入脚本时,脚本内容不能包含'</script>'字符串(会被认为是结束的标志).可以通过转义字符解决('\') 默认scri ...
- freemarket使用自定义标签 注解【项目实际使用】
页面达到效果 [主html页面代码] <!-- 合作单位 版块 -->[#include 'inc_project_succ_coo.html'/]['inc_project_succ_c ...
- Java 开发手册之编程规约
一.编程规约 (一) 命名规约 1.[强制] 代码中的命名均不能以下划线或美元符号开始,也不能以下划线或美元符号结束.(代码规范,易读) 反例: name / __name / $Object / n ...
- git身份验证失败清除密码缓存
git config --system --unset credential.helper
- Ubantu中安装sublime
Sublime-text-3的安装步骤 添加Sublime-text-3软件包的软件源 sudo add-apt-repository ppa:webupd8team/sublime-text-3 ...
- BZOJ3790:神奇项链
浅谈\(Manacher\):https://www.cnblogs.com/AKMer/p/10431603.html 题目传送门:https://lydsy.com/JudgeOnline/pro ...
- You-Get 一键下载全网视频资源
下载视频 无论是单纯的下载视频收藏,还是以便离线收看,都离不开“下载”,好的工具让你把注意力更好的放在视频的本身,而不用考虑要如何下载视频.下载视频从来不乏方法,之前也介绍了下载 Youtube ...
- UBUNTU readelf的安装
大多数情况下,linux环境上默认可能都装有readelf,但是也有少数情况可能没有装,我自己用的ubuntu的linux虚拟机就没有装readelf.readelf本身是一个分析elf很好用的工具. ...
- mongodb配置和基本操作
MongoDB3.0新特性WiredTigerMMAPv1可插拔引擎API基于web的可视化管理工具 查看版本号mongod --version启动数据库 mongod --dbpath $dbpat ...