From 501e6a4921ed2ad281c9c31c2c7991b905b5e79b Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Sat, 13 Sep 2025 07:03:04 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.3541 --- .../README.md | 48 +++++++++++++++++++ .../README_EN.md | 48 +++++++++++++++++++ .../Solution.cs | 18 +++++++ .../Solution.rs | 20 ++++++++ 4 files changed, 134 insertions(+) create mode 100644 solution/3500-3599/3541.Find Most Frequent Vowel and Consonant/Solution.cs create mode 100644 solution/3500-3599/3541.Find Most Frequent Vowel and Consonant/Solution.rs 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 1bd67f4a2638b..929174c913b06 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 @@ -169,6 +169,31 @@ func maxFreqSum(s string) int { } ``` +#### Rust + +```rust +use std::collections::HashMap; + +impl Solution { + pub fn max_freq_sum(s: String) -> i32 { + let mut cnt: HashMap = HashMap::new(); + for c in s.chars() { + *cnt.entry(c).or_insert(0) += 1; + } + let mut a = 0; + let mut b = 0; + for (c, v) in cnt { + if "aeiou".contains(c) { + a = a.max(v); + } else { + b = b.max(v); + } + } + a + b + } +} +``` + #### TypeScript ```ts @@ -190,6 +215,29 @@ function maxFreqSum(s: string): number { } ``` +#### C# + +```cs +public class Solution { + public int MaxFreqSum(string s) { + int[] cnt = new int[26]; + foreach (char c in s) { + cnt[c - 'a']++; + } + int a = 0, b = 0; + for (int i = 0; i < 26; i++) { + char c = (char)('a' + i); + 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; + } +} +``` + 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 8d7ed065617e6..d92d0b302f757 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 @@ -196,6 +196,54 @@ function maxFreqSum(s: string): number { } ``` +#### Rust + +```rust +use std::collections::HashMap; + +impl Solution { + pub fn max_freq_sum(s: String) -> i32 { + let mut cnt: HashMap = HashMap::new(); + for c in s.chars() { + *cnt.entry(c).or_insert(0) += 1; + } + let mut a = 0; + let mut b = 0; + for (c, v) in cnt { + if "aeiou".contains(c) { + a = a.max(v); + } else { + b = b.max(v); + } + } + a + b + } +} +``` + +#### C# + +```cs +public class Solution { + public int MaxFreqSum(string s) { + int[] cnt = new int[26]; + foreach (char c in s) { + cnt[c - 'a']++; + } + int a = 0, b = 0; + for (int i = 0; i < 26; i++) { + char c = (char)('a' + i); + 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; + } +} +``` + diff --git a/solution/3500-3599/3541.Find Most Frequent Vowel and Consonant/Solution.cs b/solution/3500-3599/3541.Find Most Frequent Vowel and Consonant/Solution.cs new file mode 100644 index 0000000000000..54e9bdd32244d --- /dev/null +++ b/solution/3500-3599/3541.Find Most Frequent Vowel and Consonant/Solution.cs @@ -0,0 +1,18 @@ +public class Solution { + public int MaxFreqSum(string s) { + int[] cnt = new int[26]; + foreach (char c in s) { + cnt[c - 'a']++; + } + int a = 0, b = 0; + for (int i = 0; i < 26; i++) { + char c = (char)('a' + i); + 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; + } +} diff --git a/solution/3500-3599/3541.Find Most Frequent Vowel and Consonant/Solution.rs b/solution/3500-3599/3541.Find Most Frequent Vowel and Consonant/Solution.rs new file mode 100644 index 0000000000000..a6e4e9f9825b9 --- /dev/null +++ b/solution/3500-3599/3541.Find Most Frequent Vowel and Consonant/Solution.rs @@ -0,0 +1,20 @@ +use std::collections::HashMap; + +impl Solution { + pub fn max_freq_sum(s: String) -> i32 { + let mut cnt: HashMap = HashMap::new(); + for c in s.chars() { + *cnt.entry(c).or_insert(0) += 1; + } + let mut a = 0; + let mut b = 0; + for (c, v) in cnt { + if "aeiou".contains(c) { + a = a.max(v); + } else { + b = b.max(v); + } + } + a + b + } +}