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 ...
随机推荐
- 在 CentOS 7 上安装 RabbitMQ
RabbitMQ 服务器在安装之前需要安装 erlang. 最新版本的 RabbitMQ 3.8.0 需要 Erlang 21.3 以上的版本支持. 在这里,我们需要在你的 CentOS 中安装 Er ...
- windows 安装python2.7
下载:https://www.python.org/downloads/release/python-2716/ 安装即可. 设置环境变量 进入C:\Python27,修改python.exe 为py ...
- MIME协议(五) -- MIME邮件的编码方式
5 MIME邮件的编码方式 由于每个ASCII码字符只占用一个字节(8个bit位),且最高bit位总为0,即ASCII码字符中的有真正意义的信息只是后面的7个低bit位,而传统的SMTP协议又是基于 ...
- Linux设备驱动程序 之 原子操作
原子整数操作 当共享资源是一个简单的整数值时,可以使用内核提供的一种原子的整数类型,称为atomic_t,定义在<linux/types.h>中,操作定义在<linux/atomic ...
- [Java]使用Collections.Max,Min方法取集合类的最大最小值
代码: package com.hy; import java.util.Arrays; import java.util.Collections; import java.util.List; pu ...
- 1.springboot启动流程
SpringBoot版本:2.1.2.RELEASE 1.maven <parent> <groupId>org.springframework.boot</groupI ...
- Android中的asserts和res/raw资源目录
1.assets目录 assets目录下称为原生文件,无法通过R资源清单类访问,这类文件在被打包成apk文件时是不会进行压缩的: (1)使用方法 AssetManager assets = this. ...
- 从数组中找出所有组合为s的数
java版本 package numCombine; /** * 从数组中找出所有组合为s的数 * @author root * */ public class NumComberAll { publ ...
- react+laravel与服务端渲染的几点思考
一.前后端完全分离 1.用React.js做MVC中的V,剩下的交给Laravel 2.Laravel用来做API接口开发. 3.好处:实现了前后端开发的分离,从而加快前后端开发效率.另外若是多端的如 ...
- an extra named object property
Grunt supports the ability to split each task configuration into several separate configurations all ...