public static String findLongestWord(String s, List<String> dictionary) {
String result = "";
for (String str : dictionary) {
if (str.length() > result.length() && check(str, s)) {
result = str;
} else if (str.length() == result.length() && check(str, s) && result.compareTo(str) > 0) {
result = str;
}
}
return result;
}
private static boolean check(String str, String s) {
int index = 0;
char[] charArray = str.toCharArray();
for (char character : charArray) {
if (index == s.length()) {
return false;
}
boolean flag = false;
while (index < s.length()) {
if (s.charAt(index) == character) {
index++;
flag = true;
break;
}
index++;
}
if (!flag) {
return false;
}
}
return true;
}
解题思路:
- LOW的我基本没有发现什么解题技巧;
- 主要就是判断数组中的字符串是否在给定的长串中;
- 然后在比较字典序