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

 public class Test {
public Map<Character, List<Character>> approach1(List<String> strs) {
int[] max = new int[];
int[][] cnt = new int[][]; for (int wordIdx = ; wordIdx < strs.size(); ++wordIdx) {
String word = strs.get(wordIdx); // for each word
boolean[] charExist = new boolean[];
for (char ch : word.toCharArray()) {
charExist[ch - 'a'] = true;
}
// for each combination of a-z
for (int i = ; i < ; ++i) {
for (int j = ; j < i; ++j) {
if (charExist[i] && charExist[j]) {
++cnt[i][j];
++cnt[j][i];
max[i] = Math.max(max[i], cnt[i][j]);
max[j] = Math.max(max[j], cnt[j][i]);
assert (cnt[i][j] == cnt[j][i]);
}
}
}
}
Map<Character, List<Character>> res = new HashMap<>();
for (int charId = ; charId < ; ++charId) {
if (max[charId] != ) {
List<Character> charList = new ArrayList<>();
for (int j = ; j < ; ++j) {
if (cnt[charId][j] == max[charId]) {
charList.add((char) ('a' + j));
}
}
res.put((char) ('a' + charId), charList);
}
}
return res;
} public Map<Character, List<Character>> approach2(List<String> strs) {
Map<Character, Map<Character, Integer>> mapping = new HashMap<>();
for (int wordIdx = ; wordIdx < strs.size(); ++wordIdx) {
process(strs.get(wordIdx), mapping);
} Map<Character, List<Character>> res = new HashMap<>();
mapping.entrySet().stream().forEach(entry -> {
res.put(entry.getKey(), highestFrequentCharacters(entry.getValue()));
});
return res;
} private List<Character> highestFrequentCharacters(Map<Character, Integer> map) {
List<Character> list = new ArrayList<>();
int highestCount = map.values().stream().mapToInt(count -> count.intValue()).max().getAsInt();
for (Map.Entry<Character, Integer> entry : map.entrySet()) {
if (entry.getValue() == highestCount) {
list.add(entry.getKey());
}
}
return list;
} private void process(String word, Map<Character, Map<Character, Integer>> mapping) {
Character[] uniqueChars = uniqueCharacters(word);
for (int i = ; i < uniqueChars.length; i++) {
for (int j = i + ; j < uniqueChars.length; j++) {
updateMap(mapping, uniqueChars[i], uniqueChars[j]);
updateMap(mapping, uniqueChars[j], uniqueChars[i]);
}
}
} private void updateMap(Map<Character, Map<Character, Integer>> mapping, Character firstLetter,
Character secondLetter) {
Map<Character, Integer> letterToCountMap = mapping.getOrDefault(firstLetter, new HashMap<>());
Integer count = letterToCountMap.getOrDefault(secondLetter, ) + ;
letterToCountMap.put(secondLetter, count);
mapping.put(firstLetter, letterToCountMap);
} private Character[] uniqueCharacters(String str) {
return str.chars().mapToObj(ch -> (char) ch).distinct().toArray(Character[]::new);
}
}

Highest Frequency Letters的更多相关文章

  1. 499 - What's The Frequency, Kenneth?

     What's The Frequency, Kenneth?  #include <stdio.h> main() { int i; char *suffix[]= { "st ...

  2. 九章面试题:Find first K frequency numbers 解题报告

    Find first K frequency numbers /* * Input: int[] A = {1, 1, 2, 3, 4, 5, 2}; k = 3 * return the highe ...

  3. *HDU1053 哈夫曼编码

    Entropy Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Sub ...

  4. leetcode bugfree note

    463. Island Perimeterhttps://leetcode.com/problems/island-perimeter/就是逐一遍历所有的cell,用分离的cell总的的边数减去重叠的 ...

  5. hdu 1053 Entropy

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1053 Entropy Description An entropy encoder is a data ...

  6. HDU-1053-Entropy(Huffman编码)

    Problem Description An entropy encoder is a data encoding method that achieves lossless data compres ...

  7. [POJ 1521]--Entropy(哈夫曼树)

    题目链接:http://poj.org/problem?id=1521 Entropy Time Limit: 1000MS    Memory Limit: 10000K Description A ...

  8. Problem D

    Problem Description An entropy encoder is a data encoding method that achieves lossless data compres ...

  9. Entropy

    Entropy Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...

随机推荐

  1. Vivado添加coe文件

    直接将.txt文件的后缀改为.coe,并在文件的开头添加如下两行代码即可: memory_initialization_radix=10; memory_initialization_vector=

  2. vue项目更换目录后执行npm run dev 就报错(新手进)

    在我们搭建好一个VUE项目的环境后,觉得这个项目存放的位置不好,想移动一下,但是移动后我们发现执行npm run dev就会报下面的错误: 明明只是移动了一下位置,就报错,实在是太恶心了. 但是只要我 ...

  3. ie11 div不显示背景颜色解决方案

    我的一个场景就是,一个空的div,但是想加个背景颜色,方案就是在div加个空content,利用before属性加上背景<div class="hilan"></ ...

  4. solr安装记录

    [root@localhost bin]# ./solr start -force*** [WARN] *** Your open file limit is currently 1024.   It ...

  5. Qt网络获取本机网络信息

    下面我们就讲解如何获取自己电脑的IP地址以及其他网络信息.这一节中,我们会涉及到网络模块(QtNetwork Module)中的QHostInfo ,QHostAddress ,QNetworkInt ...

  6. socket.io 的使用

    socket.io 是对 websocket 的封装,当你在客户端使用 socket.io 那么服务器也要对应的使用 目录结构: 使用方法: 客户端: socket.emit() 是提交数据,sock ...

  7. python练习-使用163邮箱发送邮件

    具体代码如下> #密码等敏感信息已经用****替换 import smtplib,sys from email.mime.text import MIMEText from email.head ...

  8. 用socket.io实现websocket的一个简单例子

    socket.io 是基于 webSocket 构建的跨浏览器的实时应用. 逛博客发现几个比较好的 一.用socket.io实现websocket的一个简单例子 http://biyeah.iteye ...

  9. mongodb的更新操作符

    mongodb更新有两个命令:1).update()命令 db.collection.update( criteria, objNew, upsert, multi ) criteria : upda ...

  10. 【深入nodejs开发】一、将node项目结合nginx部署到Centos7服务器

    一.安装nginx服务器环境 1.使用ssh工具连接服务器 2.安装宝塔面板,方便服务器管理 yum install -y wget && wget -O install.sh htt ...