Longest Word in Dictionary throu
2018-06-28 本文已影响6人
Frank_Kivi
https://www.lintcode.com/problem/longest-word-in-dictionary-through-deleting/description
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class Solution {
/**
* @param s: a string
* @param d: List[str]
* @return: return a string
*/
public String findLongestWord(String s, List<String> d) {
// write your code here
Collections.sort(d, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
if (o2.length() == o1.length()) {
return o1.compareTo(o2);
}
return o2.length() - o1.length();
}
});
test:
for (int i = 0; i < d.size(); i++) {
String s1 = d.get(i);
int index = 0;
for (char c : s1.toCharArray()) {
index = s.indexOf(c, index);
if (index < 0) {
continue test;
}
index++;
}
return s1;
}
return "";
}
}