LeetCode 3541.找到频率最高的元音和辅音:计数(位运算)
【LetMeFly】3541.找到频率最高的元音和辅音:计数(位运算)
力扣题目链接:https://leetcode.cn/problems/find-most-frequent-vowel-and-consonant/
给你一个由小写英文字母('a'
到 'z'
)组成的字符串 s
。你的任务是找出出现频率 最高 的元音('a'
、'e'
、'i'
、'o'
、'u'
中的一个)和出现频率最高的辅音(除元音以外的所有字母),并返回这两个频率之和。
注意:如果有多个元音或辅音具有相同的最高频率,可以任选其中一个。如果字符串中没有元音或没有辅音,则其频率视为 0。
一个字母x
的
频率 是它在字符串中出现的次数。
示例 1:
输入: s = "successes"
输出: 6
解释:
- 元音有:
'u'
出现 1 次,'e'
出现 2 次。最大元音频率 = 2。 - 辅音有:
's'
出现 4 次,'c'
出现 2 次。最大辅音频率 = 4。 - 输出为
2 + 4 = 6
。
示例 2:
输入: s = "aeiaeia"
输出: 3
解释:
- 元音有:
'a'
出现 3 次,'e'
出现 2 次,'i'
出现 2 次。最大元音频率 = 3。 s
中没有辅音。因此,最大辅音频率 = 0。- 输出为
3 + 0 = 3
。
提示:
1 <= s.length <= 100
s
只包含小写英文字母
解题方法:计数(位运算)
使用一个大小为26的数组记录每个字符出现了多少次(遍历一遍字符串即可得到)。
之后遍历这26种字符,统计元音字符和辅音字符分别出现了多少次。返回这两个最大值之和即可。
- 时间复杂度O(len(s)+C)O(len(s)+C)O(len(s)+C),其中C=26C=26C=26
- 空间复杂度O(C)O(C)O(C),那些所有写着O(1)O(1)O(1)的题解实际上大概也是O(C)O(C)O(C)
细节问答
- 如何判断一个字符是否为元音音符?
第一种方法就是4个或
c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'
第二种方法是位运算:
int mask = 1<<0 | 1<<4 | 1<<8 | 1<<14 | 1<<20;
int idx = mask >> i & 1; // i代表是第几个字符,从0开始计('a'对应的i=0)
这样一个字符是元音的话idx
就会为1
,否则为0
。
AC代码
C++
/** @Author: LetMeFly* @Date: 2025-09-13 16:07:42* @LastEditors: LetMeFly.xyz* @LastEditTime: 2025-09-13 16:10:22*/
class Solution {
private:inline bool isYuan(char c) {return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';}
public:int maxFreqSum(string s) {int times[26] = {0};for (char c : s) {times[c - 'a']++;}int y = 0, f = 0;for (int i = 0; i < 26; i++) {if (isYuan('a' + i)) {y = max(y, times[i]);} else {f = max(f, times[i]);}}return y + f;}
};
Python
'''
Author: LetMeFly
Date: 2025-09-13 16:07:42
LastEditors: LetMeFly.xyz
LastEditTime: 2025-09-13 16:19:20
'''
class Solution:mask = 1<<0 | 1<<4 | 1<<8 | 1<<14 | 1<<20def maxFreqSum(self, s: str) -> int:times = [0] * 26for c in s:times[ord(c) - ord('a')] += 1c = [0, 0]for i in range(26):idx = self.mask >> i & 1c[idx] = max(c[idx], times[i])return c[0] + c[1]
Java
/** @Author: LetMeFly* @Date: 2025-09-13 16:07:42* @LastEditors: LetMeFly.xyz* @LastEditTime: 2025-09-13 16:23:21*/
class Solution {private final static int mask = 1<<0 | 1<<4 | 1<<8 | 1<<14 | 1<<20;public int maxFreqSum(String s) {int[] times = new int[26];for (int i = 0; i < s.length(); i++) {times[s.charAt(i) - 'a']++;}int[] cnt = new int[2];for (int i = 0; i < 26; i++) {int idx = mask >> i & 1;cnt[idx] = Math.max(cnt[idx], times[i]);}return cnt[0] + cnt[1];}
}
Go
/** @Author: LetMeFly* @Date: 2025-09-13 16:07:42* @LastEditors: LetMeFly.xyz* @LastEditTime: 2025-09-13 16:22:43*/
package mainconst mask int = 1<<0 | 1<<4 | 1<<8 | 1<<14 | 1<<20func maxFreqSum(s string) int {times := make([]int, 26)for _, c := range s {times[byte(c) - 'a']++}cnt := []int{0, 0}for i, v := range times {idx := mask >> i & 1cnt[idx] = max(cnt[idx], v)}return cnt[0] + cnt[1]
}
Rust
/** @Author: LetMeFly* @Date: 2025-09-13 16:07:42* @LastEditors: LetMeFly.xyz* @LastEditTime: 2025-09-13 16:32:24*/
impl Solution {const MASK: usize = 1<<0 | 1<<4 | 1<<8 | 1<<14 | 1<<20;pub fn max_freq_sum(s: String) -> i32 {let mut times: Vec<i32> = vec![0; 26];for c in s.bytes() {times[(c - b'a') as usize] += 1;}let mut cnt: Vec<i32> = vec![0; 2];for i in 0..26 {let idx: usize = Self::MASK >> i & 1;cnt[idx] = cnt[idx].max(times[i]);}cnt[0] + cnt[1]}
}
同步发文于CSDN和我的个人博客,原创不易,转载经作者同意后请附上原文链接哦~
千篇源码题解已开源