Highest Frequency Letters

Given a list of strings, output the most frequent characters that are in the same group as the letter. For example, for string "abc", a, b,c are in the same group. "bcd" are in the same group. For a: b,c . for b: c as c occured in two groups with letter b. duplicates are not considered. i.e "aabc" is the same as "abc"

Example

input: a list of strings { "abc", "bcd", "def"}. 

output: a: b, c

           b: c

           c: b

           d: b, c, e, f

           e: d, f

 

 1 public class Test {
 2     public Map<Character, List<Character>> approach1(List<String> strs) {
 3         int[] max = new int[26];
 4         int[][] cnt = new int[26][26];
 5 
 6         for (int wordIdx = 0; wordIdx < strs.size(); ++wordIdx) {
 7             String word = strs.get(wordIdx); // for each word
 8             boolean[] charExist = new boolean[26];
 9             for (char ch : word.toCharArray()) {
10                 charExist[ch - 'a'] = true;
11             }
12             // for each combination of a-z
13             for (int i = 0; i < 26; ++i) {
14                 for (int j = 0; j < i; ++j) {
15                     if (charExist[i] && charExist[j]) {
16                         ++cnt[i][j];
17                         ++cnt[j][i];
18                         max[i] = Math.max(max[i], cnt[i][j]);
19                         max[j] = Math.max(max[j], cnt[j][i]);
20                         assert (cnt[i][j] == cnt[j][i]);
21                     }
22                 }
23             }
24         }
25         Map<Character, List<Character>> res = new HashMap<>();
26         for (int charId = 0; charId < 26; ++charId) {
27             if (max[charId] != 0) {
28                 List<Character> charList = new ArrayList<>();
29                 for (int j = 0; j < 26; ++j) {
30                     if (cnt[charId][j] == max[charId]) {
31                         charList.add((char) ('a' + j));
32                     }
33                 }
34                 res.put((char) ('a' + charId), charList);
35             }
36         }
37         return res;
38     }
39 
40     public Map<Character, List<Character>> approach2(List<String> strs) {
41         Map<Character, Map<Character, Integer>> mapping = new HashMap<>();
42         for (int wordIdx = 0; wordIdx < strs.size(); ++wordIdx) {
43             process(strs.get(wordIdx), mapping);
44         }
45 
46         Map<Character, List<Character>> res = new HashMap<>();
47         mapping.entrySet().stream().forEach(entry -> {
48             res.put(entry.getKey(), highestFrequentCharacters(entry.getValue()));
49         });
50         return res;
51     }
52 
53     private List<Character> highestFrequentCharacters(Map<Character, Integer> map) {
54         List<Character> list = new ArrayList<>();
55         int highestCount = map.values().stream().mapToInt(count -> count.intValue()).max().getAsInt();
56         for (Map.Entry<Character, Integer> entry : map.entrySet()) {
57             if (entry.getValue() == highestCount) {
58                 list.add(entry.getKey());
59             }
60         }
61         return list;
62     }
63 
64     private void process(String word, Map<Character, Map<Character, Integer>> mapping) {
65         Character[] uniqueChars = uniqueCharacters(word);
66         for (int i = 0; i < uniqueChars.length; i++) {
67             for (int j = i + 1; j < uniqueChars.length; j++) {
68                 updateMap(mapping, uniqueChars[i], uniqueChars[j]);
69                 updateMap(mapping, uniqueChars[j], uniqueChars[i]);
70             }
71         }
72     }
73 
74     private void updateMap(Map<Character, Map<Character, Integer>> mapping, Character firstLetter,
75             Character secondLetter) {
76         Map<Character, Integer> letterToCountMap = mapping.getOrDefault(firstLetter, new HashMap<>());
77         Integer count = letterToCountMap.getOrDefault(secondLetter, 0) + 1;
78         letterToCountMap.put(secondLetter, count);
79         mapping.put(firstLetter, letterToCountMap);
80     }
81 
82     private Character[] uniqueCharacters(String str) {
83         return str.chars().mapToObj(ch -> (char) ch).distinct().toArray(Character[]::new);
84     }
85 }

 

posted @ 2019-07-22 13:02  北叶青藤  阅读(351)  评论(0编辑  收藏  举报