public class Solution {
    /**
     * return topK string
     * @param strings string字符串一维数组 strings
     * @param k int整型 the k
     * @return string字符串二维数组
     */
    public String[][] topKstrings (String[] strings, int k) {
        // write code here
        if(k == 0){
            return new String[][]{};
        }
        String[][] result = new String[k][2];
        Map<String,Integer> map = new HashMap<>();
        for(int i = 0;i<strings.length;i++){
            String s = strings[i];
            if(!map.containsKey(s)){
                map.put(s,1);
            }else{
                map.put(s,map.get(s) +1);
            }
        }
        
        ArrayList<Map.Entry<String,Integer>> list = new ArrayList<>(map.entrySet());
        
        Collections.sort(list, new Comparator<Map.Entry<String,Integer>>() {

            @Override
            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
                 if(o1.getValue().compareTo(o2.getValue()) == 0) {
                    return o1.getKey().compareTo(o2.getKey());
                 }
                 return o2.getValue().compareTo(o1.getValue());
            }
        });;
        for(int i = 0;i< k;i++){
            result[i][0] = list.get(i).getKey();
            result[i][1] = String.valueOf(list.get(i).getValue());
        }
        return result;
    }
}