Skip to content

Commit c2b768b

Browse files
committed
Solution for: longest word dictionary through deleting
1 parent d42cdde commit c2b768b

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package leetcode;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.Collections;
6+
import java.util.List;
7+
8+
public class LongestWordDictionarythroughDeleting {
9+
10+
public static void main(String[] args) {
11+
LongestWordDictionarythroughDeleting test = new LongestWordDictionarythroughDeleting();
12+
List<String> list = new ArrayList<>(Arrays.asList(new String[]{"ale","apple","monkey","plea"}));
13+
System.out.println(test.findLongestWord("abpcplea",list));
14+
}
15+
16+
public String findLongestWord(String s, List<String> d) {
17+
Collections.sort(d, (a,b)->(a.length()==b.length())? a.compareTo(b): b.length()-a.length());
18+
for(String str: d){
19+
int cont =0;
20+
for(char c: s.toCharArray()){
21+
if(cont < str.length() && str.charAt(cont)==c){
22+
cont++;
23+
}
24+
}
25+
if(str.length()==cont) return str;
26+
}
27+
return "";
28+
}
29+
30+
}

0 commit comments

Comments
 (0)