共计 782 个字符,预计需要花费 2 分钟才能阅读完成。
要求
Input:
s = "abpcplea", d = ["ale","apple","monkey","plea"]
Output:
"apple"
实现
public class Demo7 {
@Test
public void solution1() {
String s = "abpcplea";
List<String> list = new ArrayList<>();
list.add("ale");
list.add("apple");
list.add("monkey");
list.add("plea");
String longestWord = findLongestWord(s, list);
System.out.println(longestWord);
}
private String findLongestWord(String s, List<String> d) {List max = new ArrayList();
char[] chars = s.toCharArray();
for (String s1 : d) {List<Character> list = new ArrayList<>();
char[] chars1 = s1.toCharArray();
for (int i1 = 0; i1 < chars1.length; i1++) {for (int i = i1; i < chars.length; i++) {if (chars1[i1] == chars[i]) {list.add(chars1[i1]);
break;
}
}
}
if (max.size() < list.size()) {max = list;}
}
StringBuilder stringBuilder = new StringBuilder();
max.forEach(s3 -> {stringBuilder.append(s3);
});
return stringBuilder.toString();}
}
正文完
发表至: java
2019-06-14