Skip to content

Commit 91b9162

Browse files
committed
longest word in dictionary through deleting
1 parent c315b3e commit 91b9162

File tree

3 files changed

+97
-43
lines changed

3 files changed

+97
-43
lines changed

.idea/workspace.xml

Lines changed: 48 additions & 43 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,4 +409,5 @@ codes may not be optimized
409409

410410
1. [Path Sum II](https://leetcode.com/problems/path-sum-ii/description/)
411411
1. [Minimum Moves to Equal Array Elements II](https://leetcode.com/problems/minimum-moves-to-equal-array-elements-ii/description/)
412+
1. [Longest Word in Dictionary Through Deleting](https://leetcode.com/problems/longest-word-in-dictionary-through-deleting/description/)
412413

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.github.chen0040.leetcode.day19.medium;
2+
3+
4+
import java.util.Collections;
5+
import java.util.List;
6+
7+
8+
/**
9+
* Created by xschen on 14/8/2017.
10+
*
11+
* link: https://leetcode.com/problems/longest-word-in-dictionary-through-deleting/description/
12+
*/
13+
public class LongestWordInDictionaryThroughDeleting {
14+
public class Solution {
15+
public String findLongestWord(String s, List<String> d) {
16+
Collections.sort(d);
17+
int maxLength = 0;
18+
String result = "";
19+
for(String w : d){
20+
int k = 0;
21+
boolean matched = true;
22+
for(int i=0; i < w.length(); ++i) {
23+
int c = w.charAt(i);
24+
boolean found = false;
25+
while(k < s.length()) {
26+
if(s.charAt(k) == c) {
27+
found = true;
28+
break;
29+
}
30+
k++;
31+
}
32+
if(found) k++;
33+
else {
34+
matched = false;
35+
break;
36+
}
37+
}
38+
if(matched) {
39+
if(maxLength < w.length()) {
40+
maxLength = w.length();
41+
result = w;
42+
}
43+
}
44+
}
45+
return result;
46+
}
47+
}
48+
}

0 commit comments

Comments
 (0)