class Solution {
public String longestWord(String[] words) {
Arrays.sort(words);
HashSet<String> builtWords=new HashSet();
String res="";
for(String s:words){
if(s.length()==1 || builtWords.contains(s.substring(0,s.length()-1))){
if(s.length()>res.length()) res=s;
builtWords.add(s);
}
}
return res;
}
}
leetcode 720. 词典中最长的单词
最新推荐文章于 2024-07-22 16:27:42 发布