每日打卡
720. 词典中最长的单词
class Solution {
public String longestWord(String[] words) {
// List<String> list=new ArrayList<>();
// 自排序
Arrays.sort(words);
String[] input=words;
String res="";
int n=input.length;
Set<String> set=new HashSet<>();
for(String s:input){
if(s.length()==1 || set.contains(s.substring(0,s.length()-1))){
res=s.length()>res.length()? s:res;
set.add(s);
}
}
return res;
}
}
网友评论