From 3567508aa5136f9fb27cb66869d072644ad68988 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Thu, 5 Jun 2025 06:30:55 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.3442 No.3442.Maximum Difference Between Even and Odd Frequency I --- .../README.md | 45 +++++++++++++++++++ .../README_EN.md | 45 +++++++++++++++++++ .../Solution.cs | 17 +++++++ .../Solution.rs | 18 ++++++++ 4 files changed, 125 insertions(+) create mode 100644 solution/3400-3499/3442.Maximum Difference Between Even and Odd Frequency I/Solution.cs create mode 100644 solution/3400-3499/3442.Maximum Difference Between Even and Odd Frequency I/Solution.rs diff --git a/solution/3400-3499/3442.Maximum Difference Between Even and Odd Frequency I/README.md b/solution/3400-3499/3442.Maximum Difference Between Even and Odd Frequency I/README.md index 7dede5e81a526..0b196fab0a495 100644 --- a/solution/3400-3499/3442.Maximum Difference Between Even and Odd Frequency I/README.md +++ b/solution/3400-3499/3442.Maximum Difference Between Even and Odd Frequency I/README.md @@ -187,6 +187,51 @@ function maxDifference(s: string): number { } ``` +#### Rust + +```rust +impl Solution { + pub fn max_difference(s: String) -> i32 { + let mut cnt = [0; 26]; + for c in s.bytes() { + cnt[(c - b'a') as usize] += 1; + } + let mut a = 0; + let mut b = 1 << 30; + for &v in cnt.iter() { + if v % 2 == 1 { + a = a.max(v); + } else if v > 0 { + b = b.min(v); + } + } + a - b + } +} +``` + +#### C# + +```cs +public class Solution { + public int MaxDifference(string s) { + int[] cnt = new int[26]; + foreach (char c in s) { + ++cnt[c - 'a']; + } + int a = 0, b = 1 << 30; + foreach (int v in cnt) { + if (v % 2 == 1) { + a = Math.Max(a, v); + } else if (v > 0) { + b = Math.Min(b, v); + } + } + return a - b; + } +} +``` + diff --git a/solution/3400-3499/3442.Maximum Difference Between Even and Odd Frequency I/README_EN.md b/solution/3400-3499/3442.Maximum Difference Between Even and Odd Frequency I/README_EN.md index 806b2acbdee19..8d794fabbb91f 100644 --- a/solution/3400-3499/3442.Maximum Difference Between Even and Odd Frequency I/README_EN.md +++ b/solution/3400-3499/3442.Maximum Difference Between Even and Odd Frequency I/README_EN.md @@ -185,6 +185,51 @@ function maxDifference(s: string): number { } ``` +#### Rust + +```rust +impl Solution { + pub fn max_difference(s: String) -> i32 { + let mut cnt = [0; 26]; + for c in s.bytes() { + cnt[(c - b'a') as usize] += 1; + } + let mut a = 0; + let mut b = 1 << 30; + for &v in cnt.iter() { + if v % 2 == 1 { + a = a.max(v); + } else if v > 0 { + b = b.min(v); + } + } + a - b + } +} +``` + +#### C# + +```cs +public class Solution { + public int MaxDifference(string s) { + int[] cnt = new int[26]; + foreach (char c in s) { + ++cnt[c - 'a']; + } + int a = 0, b = 1 << 30; + foreach (int v in cnt) { + if (v % 2 == 1) { + a = Math.Max(a, v); + } else if (v > 0) { + b = Math.Min(b, v); + } + } + return a - b; + } +} +``` + diff --git a/solution/3400-3499/3442.Maximum Difference Between Even and Odd Frequency I/Solution.cs b/solution/3400-3499/3442.Maximum Difference Between Even and Odd Frequency I/Solution.cs new file mode 100644 index 0000000000000..e6a7e73ec0819 --- /dev/null +++ b/solution/3400-3499/3442.Maximum Difference Between Even and Odd Frequency I/Solution.cs @@ -0,0 +1,17 @@ +public class Solution { + public int MaxDifference(string s) { + int[] cnt = new int[26]; + foreach (char c in s) { + ++cnt[c - 'a']; + } + int a = 0, b = 1 << 30; + foreach (int v in cnt) { + if (v % 2 == 1) { + a = Math.Max(a, v); + } else if (v > 0) { + b = Math.Min(b, v); + } + } + return a - b; + } +} diff --git a/solution/3400-3499/3442.Maximum Difference Between Even and Odd Frequency I/Solution.rs b/solution/3400-3499/3442.Maximum Difference Between Even and Odd Frequency I/Solution.rs new file mode 100644 index 0000000000000..751cc12a81a50 --- /dev/null +++ b/solution/3400-3499/3442.Maximum Difference Between Even and Odd Frequency I/Solution.rs @@ -0,0 +1,18 @@ +impl Solution { + pub fn max_difference(s: String) -> i32 { + let mut cnt = [0; 26]; + for c in s.bytes() { + cnt[(c - b'a') as usize] += 1; + } + let mut a = 0; + let mut b = 1 << 30; + for &v in cnt.iter() { + if v % 2 == 1 { + a = a.max(v); + } else if v > 0 { + b = b.min(v); + } + } + a - b + } +}