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. PHP mysqli_fetch_field() 函数

    返回结果集中下一字段(列),然后输出每个字段名称.表格和最大长度: <?php // 假定数据库用户名:root,密码:123456,数据库:RUNOOB $con=mysqli_connect ...

  2. CF280C

    CF280C ZR补题计划 题意: 一棵有根树,每次选择一个未删除的节点,然后删除它和它的子树内的点,问期望删多少次可以把整个树删完 解析: 显然,通过题面,我们可以知道对于一个点对 $ (u,v) ...

  3. 13.调整数组顺序使奇数位于偶数前面 Java

    思路 两次遍历,第一次把奇数加到list中,第二次把偶数加到list中,时间复杂度为O(n). 利用类似于插入排序的方法,但时间复杂度为O(n^2). 代码 import java.util.Arra ...

  4. Golang使用RabbitMQ消息中间件amqp协议

    "github.com/streadway/amqp" Publish发布 // amqp://<user>:<password>@<ip>:& ...

  5. http协议格式详解

    参考:https://www.jianshu.com/p/8fe93a14754c 一.URI结构 HTTP使用统一资源标识符(URI)来传输数据和建立连接.URL(统一资源定位符)是一种特殊种类的U ...

  6. Unity通过世界坐标系转换到界面坐标位置

    public static Vector2 WorldToCanvasPoint(Canvas canvas, Vector3 worldPos) { Vector2 pos; RectTransfo ...

  7. sentinel备忘

    git https://github.com/alibaba/Sentinel   https://github.com/dubbo/dubbo-sentinel-supportdubbo http: ...

  8. 使用vagrant一键部署本地php开发环境(二)制作自己的vagrant box

    在上篇的基础上 ,我们已经安装好了virtualbox和vagrant,没有安装的话,参照上篇 使用vagrant一键部署本地php开发环境(一) 1.从网易镜像或阿里等等镜像下载Centos7 ht ...

  9. LC 740. Delete and Earn

    Given an array nums of integers, you can perform operations on the array. In each operation, you pic ...

  10. win10下检查nvidia显卡支持的cuda版本

    1.首先将[C:\Program Files\NVIDIA Corporation\NVSMI]添加至系统环境变量[path]中: 2.在powershell中使用命令[nvidia-smi],即可看 ...