Skip to content

Commit 2e938eb

Browse files
authored
feat: add solutions to lc problem: No.3442 (#4463)
No.3442.Maximum Difference Between Even and Odd Frequency I
1 parent 6558818 commit 2e938eb

File tree

4 files changed

+125
-0
lines changed

4 files changed

+125
-0
lines changed

solution/3400-3499/3442.Maximum Difference Between Even and Odd Frequency I/README.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,51 @@ function maxDifference(s: string): number {
187187
}
188188
```
189189

190+
#### Rust
191+
192+
```rust
193+
impl Solution {
194+
pub fn max_difference(s: String) -> i32 {
195+
let mut cnt = [0; 26];
196+
for c in s.bytes() {
197+
cnt[(c - b'a') as usize] += 1;
198+
}
199+
let mut a = 0;
200+
let mut b = 1 << 30;
201+
for &v in cnt.iter() {
202+
if v % 2 == 1 {
203+
a = a.max(v);
204+
} else if v > 0 {
205+
b = b.min(v);
206+
}
207+
}
208+
a - b
209+
}
210+
}
211+
```
212+
213+
#### C#
214+
215+
```cs
216+
public class Solution {
217+
public int MaxDifference(string s) {
218+
int[] cnt = new int[26];
219+
foreach (char c in s) {
220+
++cnt[c - 'a'];
221+
}
222+
int a = 0, b = 1 << 30;
223+
foreach (int v in cnt) {
224+
if (v % 2 == 1) {
225+
a = Math.Max(a, v);
226+
} else if (v > 0) {
227+
b = Math.Min(b, v);
228+
}
229+
}
230+
return a - b;
231+
}
232+
}
233+
```
234+
190235
<!-- tabs:end -->
191236

192237
<!-- solution:end -->

solution/3400-3499/3442.Maximum Difference Between Even and Odd Frequency I/README_EN.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,51 @@ function maxDifference(s: string): number {
185185
}
186186
```
187187

188+
#### Rust
189+
190+
```rust
191+
impl Solution {
192+
pub fn max_difference(s: String) -> i32 {
193+
let mut cnt = [0; 26];
194+
for c in s.bytes() {
195+
cnt[(c - b'a') as usize] += 1;
196+
}
197+
let mut a = 0;
198+
let mut b = 1 << 30;
199+
for &v in cnt.iter() {
200+
if v % 2 == 1 {
201+
a = a.max(v);
202+
} else if v > 0 {
203+
b = b.min(v);
204+
}
205+
}
206+
a - b
207+
}
208+
}
209+
```
210+
211+
#### C#
212+
213+
```cs
214+
public class Solution {
215+
public int MaxDifference(string s) {
216+
int[] cnt = new int[26];
217+
foreach (char c in s) {
218+
++cnt[c - 'a'];
219+
}
220+
int a = 0, b = 1 << 30;
221+
foreach (int v in cnt) {
222+
if (v % 2 == 1) {
223+
a = Math.Max(a, v);
224+
} else if (v > 0) {
225+
b = Math.Min(b, v);
226+
}
227+
}
228+
return a - b;
229+
}
230+
}
231+
```
232+
188233
<!-- tabs:end -->
189234

190235
<!-- solution:end -->
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
public class Solution {
2+
public int MaxDifference(string s) {
3+
int[] cnt = new int[26];
4+
foreach (char c in s) {
5+
++cnt[c - 'a'];
6+
}
7+
int a = 0, b = 1 << 30;
8+
foreach (int v in cnt) {
9+
if (v % 2 == 1) {
10+
a = Math.Max(a, v);
11+
} else if (v > 0) {
12+
b = Math.Min(b, v);
13+
}
14+
}
15+
return a - b;
16+
}
17+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
impl Solution {
2+
pub fn max_difference(s: String) -> i32 {
3+
let mut cnt = [0; 26];
4+
for c in s.bytes() {
5+
cnt[(c - b'a') as usize] += 1;
6+
}
7+
let mut a = 0;
8+
let mut b = 1 << 30;
9+
for &v in cnt.iter() {
10+
if v % 2 == 1 {
11+
a = a.max(v);
12+
} else if v > 0 {
13+
b = b.min(v);
14+
}
15+
}
16+
a - b
17+
}
18+
}

0 commit comments

Comments
 (0)