aboutsummaryrefslogtreecommitdiff
path: root/find_frequent_vowel_consonant.c
diff options
context:
space:
mode:
authorJack Sangdahl <[email protected]>2025-09-12 22:24:30 -0600
committerJack Sangdahl <[email protected]>2025-09-12 22:24:30 -0600
commite648ecc896f5ac7db93b046699adf1832e74405f (patch)
treefec77a03df6a702c3dc35d36fb20a05605405d81 /find_frequent_vowel_consonant.c
parenta6c1d15471d1a7f32d43b9c03891fb52698de61f (diff)
downloadleetcode-e648ecc896f5ac7db93b046699adf1832e74405f.tar.gz
leetcode-e648ecc896f5ac7db93b046699adf1832e74405f.zip
3541. find most frequent vowel and consonant
Diffstat (limited to 'find_frequent_vowel_consonant.c')
-rw-r--r--find_frequent_vowel_consonant.c33
1 files changed, 33 insertions, 0 deletions
diff --git a/find_frequent_vowel_consonant.c b/find_frequent_vowel_consonant.c
new file mode 100644
index 0000000..19f18b1
--- /dev/null
+++ b/find_frequent_vowel_consonant.c
@@ -0,0 +1,33 @@
+// 3541. Find Most Frequent Vowel and Consonant
+#include "leetcode.h"
+
+/*
+ * you are given a string 's' consisting of lowercase english letters 'a' to
+ * 'z'. your task is to find the vowel with the maximum frequency, and find the
+ * consonant with the maximum frequency. return the sum of the two frequencies.
+ * note if multiple vowels or consonants have the same maximum frequency, you
+ * may choose any one of them. if there are no vowels or consonants in the
+ * string, consider their frequency as 0.
+ */
+
+int maxFreqSum(char *s) {
+ int freq[26] = {0}, n = strlen(s);
+ for (int i = 0; i < n; i++)
+ freq[s[i] - 'a']++;
+ static const int mask = 1 + (1 << ('e' - 'a')) + (1 << ('i' - 'a')) +
+ (1 << ('o' - 'a')) + (1 << ('u' - 'a'));
+ int max_v = 0, max_c = 0;
+ for (int i = 0; i < 26; i++) {
+ if (((1 << i) & mask))
+ max_v = fmax(max_v, freq[i]);
+ else
+ max_c = fmax(max_c, freq[i]);
+ }
+ return max_v + max_c;
+}
+
+int main() {
+ char *s1 = "successes", *s2 = "aeiaeia";
+ printf("%d\n", maxFreqSum(s1)); // expect: 6
+ printf("%d\n", maxFreqSum(s2)); // expect: 3
+}