class Solution {
public String findLongestWord(String s, List<String> d) {
String longest="";
for (String target:
d) {
int l1=longest.length();
int l2=target.length();
if(l1>l2||(l1==l2&&longest.compareTo(target)<0))
{continue;}
if (isSubStr(s,target)){
longest=target;
}
}
return longest;
}
public boolean isSubStr(String s,String target){
int l1=s.length(),l2=target.length();
int i=0,j=0;
while (i<l1&&j<l2){
if (s.charAt(i)==target.charAt(j)){
j++;
}
i++;
}
return j==l2;
}
}
网友评论