题目地址:
https://www.lintcode.com/problem/most-frequently-appearing-letters/description
求一个字符串 s s s里出现次数最多的字符的出现次数。
直接哈希表做统计即可。代码如下:
import java.util.HashMap;
import java.util.Map;
public class Solution {
/**
* @param str: the str
* @return: the sum that the letter appears the most
*/
public int mostFrequentlyAppearingLetters(String str) {
// Write your code here.
if (str == null || str.length() == 0) {
return 0;
}
Map<Character, Integer> map = new HashMap<>();
for (char c : str.toCharArray()) {
map.put(c, map.getOrDefault(c, 0) + 1);
}
int res = 0;
for (int value : map.values()) {
if (value > res) {
res = value;
}
}
return res;
}
}
时空复杂度 O ( l s ) O(l_s) O(ls)。