//这里意思是如果出现在一个sentence里面只算一次 public List<String> solve(int k, String[] keywords, String[] reviews) { Map<String, Integer> hm = new HashMap<>(); for (String sentence : reviews) { Set<String> hs = new HashSet<>(); String[] words = sentence.split("\\W"); for (String word : words) { hs.add(word.toLowerCase()); } for(String word : hs){ word = word.toLowerCase(); hm.put(word, hm.getOrDefault(word, 0) + 1); } } PriorityQueue<String> pq = new PriorityQueue<>((a, b) -> hm.get(a) != hm.get(b) ? hm.get(a) - hm.get(b) : b.compareTo(a)); for (String str : keywords) { str = str.toLowerCase(); if (hm.containsKey(str)) { pq.offer(str); }//if word is not in reviews need to sort alphabetically? if (pq.size() > k) { pq.poll(); } } List<String> res = new ArrayList<>(); while (!pq.isEmpty()) { res.add(pq.poll()); } Collections.reverse(res); return res; }