题目:3541. 找到频率最高的元音和辅音
思路:哈希表,记录元音和辅音字母的出现情况。时间复杂度0(n)。
C++版本:
cpp
class Solution {
public:
int maxFreqSum(string s) {
unordered_map<char,int> mp;
int mx1=0,mx2=0;
for(auto c:s){
mp[c]++;
if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u'){
mx1=max(mx1,mp[c]);
}else{
mx2=max(mx2,mp[c]);
}
}
return mx1+mx2;
}
};
JAVA版本:
java
class Solution {
public int maxFreqSum(String s) {
Map<Character,Integer> mp=new HashMap<>();
int mx1=0,mx2=0;
for(var c:s.toCharArray()){
mp.merge(c,1,Integer::sum);
if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u'){
mx1=Math.max(mx1,mp.get(c));
}else{
mx2=Math.max(mx2,mp.get(c));
}
}
return mx1+mx2;
}
}
GO版本:
go
func maxFreqSum(s string) int {
mp:=[26]int{}
mx1,mx2:=0,0
for _,c:=range s {
mp[c-'a']++;
if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u'){
mx1=max(mx1,mp[c-'a']);
}else{
mx2=max(mx2,mp[c-'a']);
}
}
return mx1+mx2;
}