From 1e2ec1a70e103e837e86337171b5ea8905c3e9aa Mon Sep 17 00:00:00 2001 From: yanglbme Date: Mon, 12 May 2025 12:30:31 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.3541 No.3541.Find Most Frequent Vowel and Consonant --- .../README.md | 96 ++++++++++++++++++- .../README_EN.md | 96 ++++++++++++++++++- .../Solution.cpp | 19 ++++ .../Solution.go | 16 ++++ .../Solution.java | 18 ++++ .../Solution.py | 10 ++ .../Solution.ts | 16 ++++ 7 files changed, 263 insertions(+), 8 deletions(-) create mode 100644 solution/3500-3599/3541.Find Most Frequent Vowel and Consonant/Solution.cpp create mode 100644 solution/3500-3599/3541.Find Most Frequent Vowel and Consonant/Solution.go create mode 100644 solution/3500-3599/3541.Find Most Frequent Vowel and Consonant/Solution.java create mode 100644 solution/3500-3599/3541.Find Most Frequent Vowel and Consonant/Solution.py create mode 100644 solution/3500-3599/3541.Find Most Frequent Vowel and Consonant/Solution.ts diff --git a/solution/3500-3599/3541.Find Most Frequent Vowel and Consonant/README.md b/solution/3500-3599/3541.Find Most Frequent Vowel and Consonant/README.md index 5e99246a91dd6..0867ce6298374 100644 --- a/solution/3500-3599/3541.Find Most Frequent Vowel and Consonant/README.md +++ b/solution/3500-3599/3541.Find Most Frequent Vowel and Consonant/README.md @@ -68,32 +68,120 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3541.Fi -### 方法一 +### 方法一:计数 + +我们先用一个哈希表或者一个长度为 $26$ 的数组 $\textit{cnt}$ 统计每个字母的出现频率。然后我们遍历这个表,找出元音和辅音中出现频率最高的字母,返回它们的频率之和。 + +我们可以用一个变量 $\textit{a}$ 记录元音的最大频率,另一个变量 $\textit{b}$ 记录辅音的最大频率。遍历时,如果当前字母是元音,就更新 $\textit{a}$;否则就更新 $\textit{b}$。 + +最后返回 $\textit{a} + \textit{b}$ 即可。 + +时间复杂度 $O(n)$,其中 $n$ 是字符串的长度。空间复杂度 $(|\Sigma|)$,其中 $|\Sigma|$ 是字母表的大小,这里是 $26$。 #### Python3 ```python - +class Solution: + def maxFreqSum(self, s: str) -> int: + cnt = Counter(s) + a = b = 0 + for c, v in cnt.items(): + if c in "aeiou": + a = max(a, v) + else: + b = max(b, v) + return a + b ``` #### Java ```java - +class Solution { + public int maxFreqSum(String s) { + int[] cnt = new int[26]; + for (char c : s.toCharArray()) { + ++cnt[c - 'a']; + } + int a = 0, b = 0; + for (int i = 0; i < cnt.length; ++i) { + char c = (char) (i + 'a'); + if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') { + a = Math.max(a, cnt[i]); + } else { + b = Math.max(b, cnt[i]); + } + } + return a + b; + } +} ``` #### C++ ```cpp - +class Solution { +public: + int maxFreqSum(string s) { + int cnt[26]{}; + for (char c : s) { + ++cnt[c - 'a']; + } + int a = 0, b = 0; + for (int i = 0; i < 26; ++i) { + char c = 'a' + i; + if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') { + a = max(a, cnt[i]); + } else { + b = max(b, cnt[i]); + } + } + return a + b; + } +}; ``` #### Go ```go +func maxFreqSum(s string) int { + cnt := [26]int{} + for _, c := range s { + cnt[c-'a']++ + } + a, b := 0, 0 + for i := range cnt { + c := byte(i + 'a') + if c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' { + a = max(a, cnt[i]) + } else { + b = max(b, cnt[i]) + } + } + return a + b +} +``` +#### TypeScript + +```ts +function maxFreqSum(s: string): number { + const cnt: number[] = Array(26).fill(0); + for (const c of s) { + ++cnt[c.charCodeAt(0) - 97]; + } + let [a, b] = [0, 0]; + for (let i = 0; i < 26; ++i) { + const c = String.fromCharCode(i + 97); + if ('aeiou'.includes(c)) { + a = Math.max(a, cnt[i]); + } else { + b = Math.max(b, cnt[i]); + } + } + return a + b; +} ``` diff --git a/solution/3500-3599/3541.Find Most Frequent Vowel and Consonant/README_EN.md b/solution/3500-3599/3541.Find Most Frequent Vowel and Consonant/README_EN.md index 20d734704e271..0f9c4460d3576 100644 --- a/solution/3500-3599/3541.Find Most Frequent Vowel and Consonant/README_EN.md +++ b/solution/3500-3599/3541.Find Most Frequent Vowel and Consonant/README_EN.md @@ -74,32 +74,120 @@ The frequency of a letter x is the number of times -### Solution 1 +### Solution 1: Counting + +We first use a hash table or an array of length $26$, $\textit{cnt}$, to count the frequency of each letter. Then, we iterate through this table to find the most frequent vowel and consonant, and return the sum of their frequencies. + +We can use a variable $\textit{a}$ to record the maximum frequency of vowels and another variable $\textit{b}$ to record the maximum frequency of consonants. During the iteration, if the current letter is a vowel, we update $\textit{a}$; otherwise, we update $\textit{b}$. + +Finally, we return $\textit{a} + \textit{b}$. + +The time complexity is $O(n)$, where $n$ is the length of the string. The space complexity is $O(|\Sigma|)$, where $|\Sigma|$ is the size of the alphabet, which is $26$ in this case. #### Python3 ```python - +class Solution: + def maxFreqSum(self, s: str) -> int: + cnt = Counter(s) + a = b = 0 + for c, v in cnt.items(): + if c in "aeiou": + a = max(a, v) + else: + b = max(b, v) + return a + b ``` #### Java ```java - +class Solution { + public int maxFreqSum(String s) { + int[] cnt = new int[26]; + for (char c : s.toCharArray()) { + ++cnt[c - 'a']; + } + int a = 0, b = 0; + for (int i = 0; i < cnt.length; ++i) { + char c = (char) (i + 'a'); + if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') { + a = Math.max(a, cnt[i]); + } else { + b = Math.max(b, cnt[i]); + } + } + return a + b; + } +} ``` #### C++ ```cpp - +class Solution { +public: + int maxFreqSum(string s) { + int cnt[26]{}; + for (char c : s) { + ++cnt[c - 'a']; + } + int a = 0, b = 0; + for (int i = 0; i < 26; ++i) { + char c = 'a' + i; + if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') { + a = max(a, cnt[i]); + } else { + b = max(b, cnt[i]); + } + } + return a + b; + } +}; ``` #### Go ```go +func maxFreqSum(s string) int { + cnt := [26]int{} + for _, c := range s { + cnt[c-'a']++ + } + a, b := 0, 0 + for i := range cnt { + c := byte(i + 'a') + if c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' { + a = max(a, cnt[i]) + } else { + b = max(b, cnt[i]) + } + } + return a + b +} +``` +#### TypeScript + +```ts +function maxFreqSum(s: string): number { + const cnt: number[] = Array(26).fill(0); + for (const c of s) { + ++cnt[c.charCodeAt(0) - 97]; + } + let [a, b] = [0, 0]; + for (let i = 0; i < 26; ++i) { + const c = String.fromCharCode(i + 97); + if ('aeiou'.includes(c)) { + a = Math.max(a, cnt[i]); + } else { + b = Math.max(b, cnt[i]); + } + } + return a + b; +} ``` diff --git a/solution/3500-3599/3541.Find Most Frequent Vowel and Consonant/Solution.cpp b/solution/3500-3599/3541.Find Most Frequent Vowel and Consonant/Solution.cpp new file mode 100644 index 0000000000000..2b39e0293f329 --- /dev/null +++ b/solution/3500-3599/3541.Find Most Frequent Vowel and Consonant/Solution.cpp @@ -0,0 +1,19 @@ +class Solution { +public: + int maxFreqSum(string s) { + int cnt[26]{}; + for (char c : s) { + ++cnt[c - 'a']; + } + int a = 0, b = 0; + for (int i = 0; i < 26; ++i) { + char c = 'a' + i; + if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') { + a = max(a, cnt[i]); + } else { + b = max(b, cnt[i]); + } + } + return a + b; + } +}; \ No newline at end of file diff --git a/solution/3500-3599/3541.Find Most Frequent Vowel and Consonant/Solution.go b/solution/3500-3599/3541.Find Most Frequent Vowel and Consonant/Solution.go new file mode 100644 index 0000000000000..0d0c9a3143242 --- /dev/null +++ b/solution/3500-3599/3541.Find Most Frequent Vowel and Consonant/Solution.go @@ -0,0 +1,16 @@ +func maxFreqSum(s string) int { + cnt := [26]int{} + for _, c := range s { + cnt[c-'a']++ + } + a, b := 0, 0 + for i := range cnt { + c := byte(i + 'a') + if c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' { + a = max(a, cnt[i]) + } else { + b = max(b, cnt[i]) + } + } + return a + b +} \ No newline at end of file diff --git a/solution/3500-3599/3541.Find Most Frequent Vowel and Consonant/Solution.java b/solution/3500-3599/3541.Find Most Frequent Vowel and Consonant/Solution.java new file mode 100644 index 0000000000000..0106eb7261e13 --- /dev/null +++ b/solution/3500-3599/3541.Find Most Frequent Vowel and Consonant/Solution.java @@ -0,0 +1,18 @@ +class Solution { + public int maxFreqSum(String s) { + int[] cnt = new int[26]; + for (char c : s.toCharArray()) { + ++cnt[c - 'a']; + } + int a = 0, b = 0; + for (int i = 0; i < cnt.length; ++i) { + char c = (char) (i + 'a'); + if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') { + a = Math.max(a, cnt[i]); + } else { + b = Math.max(b, cnt[i]); + } + } + return a + b; + } +} \ No newline at end of file diff --git a/solution/3500-3599/3541.Find Most Frequent Vowel and Consonant/Solution.py b/solution/3500-3599/3541.Find Most Frequent Vowel and Consonant/Solution.py new file mode 100644 index 0000000000000..02fcacfd994f5 --- /dev/null +++ b/solution/3500-3599/3541.Find Most Frequent Vowel and Consonant/Solution.py @@ -0,0 +1,10 @@ +class Solution: + def maxFreqSum(self, s: str) -> int: + cnt = Counter(s) + a = b = 0 + for c, v in cnt.items(): + if c in "aeiou": + a = max(a, v) + else: + b = max(b, v) + return a + b diff --git a/solution/3500-3599/3541.Find Most Frequent Vowel and Consonant/Solution.ts b/solution/3500-3599/3541.Find Most Frequent Vowel and Consonant/Solution.ts new file mode 100644 index 0000000000000..151456f4f4d08 --- /dev/null +++ b/solution/3500-3599/3541.Find Most Frequent Vowel and Consonant/Solution.ts @@ -0,0 +1,16 @@ +function maxFreqSum(s: string): number { + const cnt: number[] = Array(26).fill(0); + for (const c of s) { + ++cnt[c.charCodeAt(0) - 97]; + } + let [a, b] = [0, 0]; + for (let i = 0; i < 26; ++i) { + const c = String.fromCharCode(i + 97); + if ('aeiou'.includes(c)) { + a = Math.max(a, cnt[i]); + } else { + b = Math.max(b, cnt[i]); + } + } + return a + b; +}