1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
| class Solution { int n = 100010; int[][] st = new int[n][26]; int[] count = new int[n]; int idx = 0; Map<String, Integer> map = new HashMap<>(); public List<String> topKFrequent(String[] words, int k) { for (String str : words) { insert(str); }
PriorityQueue<String> queue = new PriorityQueue<>(k, (o1,o2)->{ if (map.get(o2) == map.get(o1)){ return o1.compareTo(o2); } return map.get(o2) - map.get(o1); });
for (String str : words) { int cnt = search(str); map.put(str, cnt); if (!queue.contains(str)) { queue.offer(str); } }
List<String> res = new ArrayList<>(); while (k > 0){ res.add(queue.poll()); k--; } return res; }
public void insert(String str){ int p = 0; for (int i = 0; i < str.length(); i++) { int j = str.charAt(i) - 'a'; if (st[p][j] == 0) { st[p][j] = ++idx; } p = st[p][j]; } count[p]++; }
public int search(String str){ int p = 0; for (int i = 0; i < str.length(); i++) { int j = str.charAt(i) - 'a'; if (st[p][j] == 0) { return 0; } p = st[p][j]; } return count[p]; } }
|