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 ...
随机推荐
- CVE-2019-0708复现
本人在此申明: 此次复现仅供学习使用 不可用于非法用途 一切违法后果与本人无关 复现0708第一步 github下载exp Kali里面执行命令 wget https://raw.githubuser ...
- 安装完Pycharm,启动时碰到"failed to load jvm dll"的解决方案
今天安装完系统,配置pycharm的环境的时候,启动pycharm时,碰到"failed to load jvm dll"的错误, 下面给出其解决方案: 安装Microsoft V ...
- 【redis 学习系列07】Redis小功能大用处01 慢查询分析以及Redis Shell
Redis提供了5种数据结构已经足够强大,但除此之外,Redis还提供了诸如慢查询分析.功能强大的Redis Shell.Pipeline.事务与Lua脚本.Bitmaps.HyperLogLog.发 ...
- C#_switch语句,for循环,do while循环,while循环
1:switch语句,代码如下: using System; using System.Collections.Generic; using System.Linq; using System.Tex ...
- 20191121-5 Scrum立会报告+燃尽图 02
此作业要求参见https://edu.cnblogs.com/campus/nenu/2019fall/homework/10066 一.小组情况 组长:贺敬文组员:彭思雨 王志文 位军营 徐丽君队名 ...
- 黑马vue---20、v-if和v-show的使用和特点
黑马vue---20.v-if和v-show的使用和特点 一.总结 一句话总结: v-if 的特点:每次都会重新删除或创建元素 v-show 的特点: 每次不会重新进行DOM的删除和创建操作,只是切换 ...
- Qt多线程应用--QRunnable
http://blog.csdn.net/lefttime/article/details/5717349 作为Qt类中少有的基类, QRunnable提供了简洁有效的可运行对象的创建. 用QRun ...
- 阿里云AHAS应用高可用服务初体验
AHAS是阿里云提供的应用高可用服务(Application High Availability Service)产品. 高可用这个关键词可以说是互联网及软件开发行业热度一直很高的词语了,阿里云推出的 ...
- DP&图论 DAY 7 上午
DP&图论 DAY 7 上午 图论练习题 P2176 [USACO14FEB]路障Roadblock 先跑最短路(最多n条边,否则出环) 枚举每条边,加倍,再跑 dijkstra 取最大 ...
- easyUI之Tree(树)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <hea ...