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 "";
}
}
网友评论