package leetcode;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * https://leetcode.com/problems/top-k-frequent-words/
 */
public class Problem692 {
    public List<String> topKFrequent(String[] words, int k) {
        Map<String, WordCount> wordCounts = new HashMap<>();
        for (String word : words) {
            if (!wordCounts.containsKey(word)) {
                wordCounts.put(word, new WordCount(word));
            } else {
                wordCounts.get(word).count++;
            }
        }
        List<WordCount> values = new ArrayList<>(wordCounts.values());
        Collections.sort(values, (a, b) -> {
            int cmp = Integer.compare(b.count, a.count);
            if (cmp == 0) {
                return a.word.compareTo(b.word);
            }
            return cmp;
        });
        List<String> result = new ArrayList<>();
        for (int i = 0; i < k; i++) {
            result.add(values.get(i).word);
        }
        return result;
    }

    private static class WordCount {
        private final String word;
        private int count;

        public WordCount(String word) {
            this.word = word;
        }
    }

    public static void main(String[] args) {
        Problem692 prob = new Problem692();
        System.out.println(prob.topKFrequent(new String[]{
            "i", "love", "leetcode", "i", "love", "coding"}, 2)); // ["i", "love"]
        System.out.println(prob.topKFrequent(new String[]{
            "the", "day", "is", "sunny", "the", "the", "the", "sunny", "is", "is"
        }, 4)); // ["the", "is", "sunny", "day"]
    }
}

@@ -140,16 +140,44 @@
 til.Map;
+%0Aimport java.util.Map.Entry;
 %0A%0A/**%0A *
@@ -341,25 +341,23 @@
 String, 
-WordCount
+Integer
 %3E wordCo
@@ -506,27 +506,9 @@
 rd, 
-new WordCount(word)
+1
 );%0A 
@@ -558,25 +558,43 @@
 nts.
-ge
+pu
 t(word
-).count++
+, wordCounts.get(word) + 1)
 ;%0A  
@@ -632,23 +632,37 @@
 ist%3C
-WordCount%3E valu
+Entry%3CString, Integer%3E%3E entri
 es =
@@ -689,22 +689,24 @@
 dCounts.
-values
+entrySet
 ());%0A   
@@ -727,20 +727,21 @@
 ns.sort(
-valu
+entri
 es, (a, 
@@ -792,22 +792,32 @@
 e(b.
-count, a.count
+getValue(), a.getValue()
 );%0A 
@@ -868,20 +868,24 @@
 eturn a.
-word
+getKey()
 .compare
@@ -889,20 +889,24 @@
 areTo(b.
-word
+getKey()
 );%0A     
@@ -1064,676 +1064,66 @@
 add(
-values.get(i).word);%0A        %7D%0A        return result;%0A    %7D%0A%0A    private static class WordCount %7B%0A        private final String word;%0A        private int count;%0A%0A        public WordCount(String word) %7B%0A            this.word = word;%0A        %7D%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem692 prob = new Problem692();%0A        System.out.println(prob.topKFrequent(new String%5B%5D%7B%0A            %22i%22, %22love%22, %22leetcode%22, %22i%22, %22love%22, %22coding%22%7D, 2)); // %5B%22i%22, %22love%22%5D%0A        System.out.println(prob.topKFrequent(new String%5B%5D%7B%0A            %22the%22, %22day%22, %22is%22, %22sunny%22, %22the%22, %22the%22, %22the%22, %22sunny%22, %22is%22, %22is%22%0A        %7D, 4)); // %5B%22the%22, %22is%22, %22sunny%22, %22day%22%5D
+entries.get(i).getKey());%0A        %7D%0A        return result;
 %0A   

Solve problem 692