Longest Word in Dictionary

找到最长的字符串, 这个字符串的前缀字符串都在给出的字符串组里. 这个直接做吧…我看好多人用trie…懒得用了.

class Solution {
    public String longestWord(String[] words) {
        Set<String> set = new HashSet<>();
        for(String w : words)
            set.add(w);
        PriorityQueue<String> pq = new PriorityQueue<String>(new Comparator<String>() {
            public int compare(String s1, String s2) {
                if(s1.length() != s2.length()) // if len is not equal
                    return s2.length() - s1.length(); // letx order
                else {
                    return s1.compareTo(s2);
                }
            }
        });
        for(String w : words) {
            int len = w.length();
            boolean check = true;
            for(int i = 1; i < len; i++) {
                if(!set.contains(w.substring(0,i))) {
                    check = false;
                    break;
                }
            }
            if (check)
                pq.add(w);
        }
        return pq.peek();
    }
}