题目:
给你一个字符串 s
和一个字符串数组 dictionary
,找出并返回 dictionary
中最长的字符串,该字符串可以通过删除 s
中的某些字符得到。
如果答案不止一个,返回长度最长且字母序最小的字符串。如果答案不存在,则返回空字符串。
思路:双指针
代码:
java
class Solution {
public String findLongestWord(String s, List<String> dictionary) {
String res = "";
for (String t : dictionary) {
int i = 0;
int j = 0;
while (i < t.length() && j < s.length()) {
if (t.charAt(i) == s.charAt(j)) {
i++;
}
j++;
}
if (i == t.length()) {
if (t.length() > res.length() || t.length() == res.length() && t.compareTo(res) < 0) {
res = t;
}
}
}
return res;
}
}
性能:
