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
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的更多相关文章
- 499 - What's The Frequency, Kenneth?
What's The Frequency, Kenneth? #include <stdio.h> main() { int i; char *suffix[]= { "st ...
- 九章面试题: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 ...
- *HDU1053 哈夫曼编码
Entropy Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Sub ...
- leetcode bugfree note
463. Island Perimeterhttps://leetcode.com/problems/island-perimeter/就是逐一遍历所有的cell,用分离的cell总的的边数减去重叠的 ...
- hdu 1053 Entropy
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1053 Entropy Description An entropy encoder is a data ...
- HDU-1053-Entropy(Huffman编码)
Problem Description An entropy encoder is a data encoding method that achieves lossless data compres ...
- [POJ 1521]--Entropy(哈夫曼树)
题目链接:http://poj.org/problem?id=1521 Entropy Time Limit: 1000MS Memory Limit: 10000K Description A ...
- Problem D
Problem Description An entropy encoder is a data encoding method that achieves lossless data compres ...
- Entropy
Entropy Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
随机推荐
- mov 与 lea 区别
转自:https://blog.csdn.net/fengyuanye/article/details/85715565 https://my.oschina.net/guonaihong/blog/ ...
- 网络层中的 IP 协议
IP协议 IP(IPv4.IPv6)相当于 OSI 参考模型中的第3层——网络层.网络层的主要作用是“实现终端节点之间的通信”.这种终端节点之间的通信也叫“点对点通信”. 网络的下一层——数据链路层的 ...
- AcWing:176. 装满的油箱(bfs + dijiskla思想)
有N个城市(编号0.1…N-1)和M条道路,构成一张无向图. 在每个城市里边都有一个加油站,不同的加油站的单位油价不一样. 现在你需要回答不超过100个问题,在每个问题中,请计算出一架油箱容量为C的车 ...
- selenium的方法
# Licensed to the Software Freedom Conservancy (SFC) under one # or more contributor license agreeme ...
- 外网访问内网的FTP服务器
转自 外网访问内网的FTP服务器 首先感谢作者给出的总结,原文是介绍Serv-U的,我针对FileZilla Server进行了稍微修改,仅看操作可直接跳到分割线后第3部分. 1. 背景简介最近研究如 ...
- zookeeper备忘
ZooInspector https://issues.apache.org/jira/secure/attachment/12436620/ZooInspector.zip 参考:https://b ...
- start-20180323
几年前申请了博客,http://www.cnblogs.com/cdfive/,一篇文章没写-_-|| 账号都忘了orz.. 又到了离职的时候,开始重新找工作: 昨天一家平台好的单位面试没过,可能是跳 ...
- @Transactional(事务讲解)和springboot 整合事务
概述 事务在编程中分为两种:声明式事务处理和编程式事务处理 编程式事务处理:编码方式实现事务管理,常与模版类TransactionTemplate(推荐使用) 在业务代码中实现事务. 可知编程式事务每 ...
- Jmeter 时间函数
1.参数值是日期,而日期是当前时间:用__time函数,生成任意格式时间 把生成的函数字符串直接复制粘贴即可使用 2.参数是昨天或者明天,即以当前时间为基准,增加或减少固定时间的,可以用__timeS ...
- 用es6实现一个promsie
Promise 使用方法:https://www.runoob.com/w3cnote/javascript-promise-object.html 直接上代码,相关的解释都在代码的注释里面,这里以m ...