Skip to content

Commit fb4033b

Browse files
authored
update: 添加问题“3442.奇偶频次间的最大差值I”的代码和题解(#979)
* 3442: WA.cpp (#978) what? 这能WA Signed-off-by: LetMeFly666 <[email protected]> * 3442: AC.cpp (#978) - AC,100.00%,77.93% 注意判最小偶的时候要满足非零 Signed-off-by: LetMeFly666 <[email protected]> * 3442: AC.Cpp+Py+Go{Java (#978) int数组竟然也能 for(int t : a) cpp - AC,35.86%,85.52% py - AC,77.03%,77.03% go - AC,100.00%,62.50% java - AC,100.00%,57.67% Signed-off-by: LetMeFly666 <[email protected]> * MGJW+words: many (#978) * update: 添加问题“3442.奇偶频次间的最大差值I”的代码和题解(#979) Signed-off-by: LetMeFly666 <[email protected]> --------- Signed-off-by: LetMeFly666 <[email protected]>
1 parent 3c4a791 commit fb4033b

18 files changed

+276
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-06-10 23:07:14
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-06-10 23:14:08
6+
*/
7+
#if defined(_WIN32) || defined(__APPLE__)
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
class Solution {
12+
public:
13+
int maxDifference(string s) {
14+
int cnt[26] = {0};
15+
for (char c : s) {
16+
cnt[c - 'a']++;
17+
}
18+
int a1 = 0, a2 = 100;
19+
for (int c : cnt) {
20+
if (c % 2) {
21+
a1 = max(a1, c);
22+
} else if (c) {
23+
a2 = min(a2, c);
24+
}
25+
}
26+
return a1 - a2;
27+
}
28+
};
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-06-10 23:07:14
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-06-10 23:20:56
6+
*/
7+
package main
8+
9+
func maxDifference(s string) int {
10+
cnt := [26]int{}
11+
for _, c := range s {
12+
cnt[c - 'a']++
13+
}
14+
a1, a2 := 0, 100
15+
for _, t := range cnt {
16+
if t % 2 == 1 {
17+
a1 = max(a1, t)
18+
} else if t > 0 {
19+
a2 = min(a2, t)
20+
}
21+
}
22+
return a1 - a2
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-06-10 23:07:14
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-06-10 23:40:04
6+
*/
7+
class Solution {
8+
public int maxDifference(String s) {
9+
int[] cnt = new int[26];
10+
for (char c : s.toCharArray()) {
11+
cnt[c - 'a']++;
12+
}
13+
int a1 = 0, a2 = 100;
14+
for (int t : cnt) {
15+
if (t % 2 == 1) {
16+
a1 = Math.max(a1, t);
17+
} else if (t > 0) {
18+
a2 = Math.min(a2, t);
19+
}
20+
}
21+
return a1 - a2;
22+
}
23+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2025-06-10 23:07:14
4+
LastEditors: LetMeFly.xyz
5+
LastEditTime: 2025-06-10 23:18:45
6+
'''
7+
from collections import Counter
8+
9+
class Solution:
10+
def maxDifference(self, s: str) -> int:
11+
cnt = Counter(s)
12+
return max(v for v in cnt.values() if v % 2) - min(v for v in cnt.values() if v % 2 == 0)

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -986,6 +986,7 @@
986986
|3375.使数组的值全部为K的最少操作次数|简单|<a href="https://leetcode.cn/problems/minimum-operations-to-make-array-values-equal-to-k/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/04/09/LeetCode%203375.%E4%BD%BF%E6%95%B0%E7%BB%84%E7%9A%84%E5%80%BC%E5%85%A8%E9%83%A8%E4%B8%BAK%E7%9A%84%E6%9C%80%E5%B0%91%E6%93%8D%E4%BD%9C%E6%AC%A1%E6%95%B0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/147104288" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/minimum-operations-to-make-array-values-equal-to-k/solutions/3646138/letmefly-3375shi-shu-zu-de-zhi-quan-bu-w-i71e/" target="_blank">LeetCode题解</a>|
987987
|3392.统计符合条件长度为3的子数组数目|简单|<a href="https://leetcode.cn/problems/count-subarrays-of-length-three-with-a-condition/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/04/29/LeetCode%203392.%E7%BB%9F%E8%AE%A1%E7%AC%A6%E5%90%88%E6%9D%A1%E4%BB%B6%E9%95%BF%E5%BA%A6%E4%B8%BA3%E7%9A%84%E5%AD%90%E6%95%B0%E7%BB%84%E6%95%B0%E7%9B%AE/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/147603015" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/count-subarrays-of-length-three-with-a-condition/solutions/3665081/letmefly-3392tong-ji-fu-he-tiao-jian-cha-h935/" target="_blank">LeetCode题解</a>|
988988
|3396.使数组元素互不相同所需的最少操作次数|简单|<a href="https://leetcode.cn/problems/minimum-number-of-operations-to-make-elements-in-array-distinct/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/04/08/LeetCode%203396.%E4%BD%BF%E6%95%B0%E7%BB%84%E5%85%83%E7%B4%A0%E4%BA%92%E4%B8%8D%E7%9B%B8%E5%90%8C%E6%89%80%E9%9C%80%E7%9A%84%E6%9C%80%E5%B0%91%E6%93%8D%E4%BD%9C%E6%AC%A1%E6%95%B0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/147080178" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/minimum-number-of-operations-to-make-elements-in-array-distinct/solutions/3644951/letmefly-3396shi-shu-zu-yuan-su-hu-bu-xi-glg4/" target="_blank">LeetCode题解</a>|
989+
|3442.奇偶频次间的最大差值I|简单|<a href="https://leetcode.cn/problems/maximum-difference-between-even-and-odd-frequency-i/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/06/10/LeetCode%203442.%E5%A5%87%E5%81%B6%E9%A2%91%E6%AC%A1%E9%97%B4%E7%9A%84%E6%9C%80%E5%A4%A7%E5%B7%AE%E5%80%BCI/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/148570777" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/maximum-difference-between-even-and-odd-frequency-i/solutions/3697605/letmefly-3442qi-ou-pin-ci-jian-de-zui-da-nwwx/" target="_blank">LeetCode题解</a>|
989990
|剑指Offer0047.礼物的最大价值|简单|<a href="https://leetcode.cn/problems/li-wu-de-zui-da-jie-zhi-lcof/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/03/08/LeetCode%20%E5%89%91%E6%8C%87%20Offer%2047.%20%E7%A4%BC%E7%89%A9%E7%9A%84%E6%9C%80%E5%A4%A7%E4%BB%B7%E5%80%BC/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/129408765" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/li-wu-de-zui-da-jie-zhi-lcof/solutions/2155672/letmefly-jian-zhi-offer-47li-wu-de-zui-d-rekb/" target="_blank">LeetCode题解</a>|
990991
|剑指OfferII0041.滑动窗口的平均值|简单|<a href="https://leetcode.cn/problems/qIsx9U/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/07/16/LeetCode%20%E5%89%91%E6%8C%87%20Offer%20II%200041.%20%E6%BB%91%E5%8A%A8%E7%AA%97%E5%8F%A3%E7%9A%84%E5%B9%B3%E5%9D%87%E5%80%BC/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/125819216" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/qIsx9U/solution/by-tisfy-30mq/" target="_blank">LeetCode题解</a>|
991992
|剑指OfferII0091.粉刷房子|中等|<a href="https://leetcode.cn/problems/JEj789/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/06/25/LeetCode%20%E5%89%91%E6%8C%87%20Offer%20II%200091.%20%E7%B2%89%E5%88%B7%E6%88%BF%E5%AD%90/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/125456885" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/JEj789/solution/letmefly-jian-zhi-offer-ii-091fen-shua-f-3olz/" target="_blank">LeetCode题解</a>|
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
---
2+
title: 3442.奇偶频次间的最大差值 I:计数
3+
date: 2025-06-10 23:41:55
4+
tags: [题解, LeetCode, 简单, 哈希表, 字符串, 计数]
5+
categories: [题解, LeetCode]
6+
---
7+
8+
# 【LetMeFly】3442.奇偶频次间的最大差值 I:计数
9+
10+
力扣题目链接:[https://leetcode.cn/problems/maximum-difference-between-even-and-odd-frequency-i/](https://leetcode.cn/problems/maximum-difference-between-even-and-odd-frequency-i/)
11+
12+
<p>给你一个由小写英文字母组成的字符串&nbsp;<code>s</code> 。请你找出字符串中两个字符的出现频次之间的 <strong>最大</strong> 差值,这两个字符需要满足:</p>
13+
14+
<ul>
15+
<li>一个字符在字符串中出现 <strong>偶数次</strong> 。</li>
16+
<li>另一个字符在字符串中出现 <strong>奇数次</strong>&nbsp;。</li>
17+
</ul>
18+
19+
<p>返回 <strong>最大</strong> 差值,计算方法是出现 <strong>奇数次</strong> 字符的次数 <strong>减去</strong> 出现 <strong>偶数次</strong> 字符的次数。</p>
20+
21+
<p>&nbsp;</p>
22+
23+
<p><b>示例 1:</b></p>
24+
25+
<div class="example-block">
26+
<p><span class="example-io"><b>输入:</b>s = "aaaaabbc"</span></p>
27+
28+
<p><b>输出:</b>3</p>
29+
30+
<p><b>解释:</b></p>
31+
32+
<ul>
33+
<li>字符&nbsp;<code>'a'</code>&nbsp;出现 <strong>奇数次</strong>&nbsp;,次数为&nbsp;<code><font face="monospace">5</font></code><font face="monospace"> ;字符</font>&nbsp;<code>'b'</code>&nbsp;出现 <strong>偶数次</strong> ,次数为&nbsp;<code><font face="monospace">2</font></code>&nbsp;。</li>
34+
<li>最大差值为&nbsp;<code>5 - 2 = 3</code>&nbsp;。</li>
35+
</ul>
36+
</div>
37+
38+
<p><b>示例 2:</b></p>
39+
40+
<div class="example-block">
41+
<p><span class="example-io"><b>输入:</b>s = "abcabcab"</span></p>
42+
43+
<p><b>输出:</b>1</p>
44+
45+
<p><b>解释:</b></p>
46+
47+
<ul>
48+
<li>字符&nbsp;<code>'a'</code>&nbsp;出现 <strong>奇数次</strong>&nbsp;,次数为&nbsp;<code><font face="monospace">3</font></code><font face="monospace"> ;字符</font>&nbsp;<code>'c'</code>&nbsp;出现 <strong>偶数次</strong>&nbsp;,次数为&nbsp;<font face="monospace">2 。</font></li>
49+
<li>最大差值为&nbsp;<code>3 - 2 = 1</code> 。</li>
50+
</ul>
51+
</div>
52+
53+
<p>&nbsp;</p>
54+
55+
<p><b>提示:</b></p>
56+
57+
<ul>
58+
<li><code>3 &lt;= s.length &lt;= 100</code></li>
59+
<li><code>s</code>&nbsp;仅由小写英文字母组成。</li>
60+
<li><code>s</code>&nbsp;至少由一个出现奇数次的字符和一个出现偶数次的字符组成。</li>
61+
</ul>
62+
63+
64+
65+
## 解题方法:计数
66+
67+
遍历一遍字符串,统计出每种字符的出现次数。
68+
69+
遍历英文单词的每种字符:
70+
71+
+ 若这种字符出现次数为奇数次,则更新a1的值
72+
+ 否则若这种字符出现次数大于0,则更新a2的值
73+
74+
最终返回$a_1-a_2$。
75+
76+
+ 时间复杂度$O(len(s)+C)$,其中$C=26$
77+
+ 空间复杂度$O(C)$
78+
79+
### AC代码
80+
81+
#### C++
82+
83+
```cpp
84+
/*
85+
* @Author: LetMeFly
86+
* @Date: 2025-06-10 23:07:14
87+
* @LastEditors: LetMeFly.xyz
88+
* @LastEditTime: 2025-06-10 23:14:08
89+
*/
90+
class Solution {
91+
public:
92+
int maxDifference(string s) {
93+
int cnt[26] = {0};
94+
for (char c : s) {
95+
cnt[c - 'a']++;
96+
}
97+
int a1 = 0, a2 = 100;
98+
for (int c : cnt) {
99+
if (c % 2) {
100+
a1 = max(a1, c);
101+
} else if (c) {
102+
a2 = min(a2, c);
103+
}
104+
}
105+
return a1 - a2;
106+
}
107+
};
108+
```
109+
110+
#### Python
111+
112+
```python
113+
'''
114+
Author: LetMeFly
115+
Date: 2025-06-10 23:07:14
116+
LastEditors: LetMeFly.xyz
117+
LastEditTime: 2025-06-10 23:18:45
118+
'''
119+
from collections import Counter
120+
121+
class Solution:
122+
def maxDifference(self, s: str) -> int:
123+
cnt = Counter(s)
124+
return max(v for v in cnt.values() if v % 2) - min(v for v in cnt.values() if v % 2 == 0)
125+
```
126+
127+
#### Java
128+
129+
```java
130+
/*
131+
* @Author: LetMeFly
132+
* @Date: 2025-06-10 23:07:14
133+
* @LastEditors: LetMeFly.xyz
134+
* @LastEditTime: 2025-06-10 23:40:04
135+
*/
136+
class Solution {
137+
public int maxDifference(String s) {
138+
int[] cnt = new int[26];
139+
for (char c : s.toCharArray()) {
140+
cnt[c - 'a']++;
141+
}
142+
int a1 = 0, a2 = 100;
143+
for (int t : cnt) {
144+
if (t % 2 == 1) {
145+
a1 = Math.max(a1, t);
146+
} else if (t > 0) {
147+
a2 = Math.min(a2, t);
148+
}
149+
}
150+
return a1 - a2;
151+
}
152+
}
153+
```
154+
155+
#### Go
156+
157+
```go
158+
/*
159+
* @Author: LetMeFly
160+
* @Date: 2025-06-10 23:07:14
161+
* @LastEditors: LetMeFly.xyz
162+
* @LastEditTime: 2025-06-10 23:20:56
163+
*/
164+
package main
165+
166+
func maxDifference(s string) int {
167+
cnt := [26]int{}
168+
for _, c := range s {
169+
cnt[c - 'a']++
170+
}
171+
a1, a2 := 0, 100
172+
for _, t := range cnt {
173+
if t % 2 == 1 {
174+
a1 = max(a1, t)
175+
} else if t > 0 {
176+
a2 = min(a2, t)
177+
}
178+
}
179+
return a1 - a2
180+
}
181+
```
182+
183+
> 同步发文于[CSDN](https://letmefly.blog.csdn.net/article/details/148570777)和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2025/06/10/LeetCode%203442.%E5%A5%87%E5%81%B6%E9%A2%91%E6%AC%A1%E9%97%B4%E7%9A%84%E6%9C%80%E5%A4%A7%E5%B7%AE%E5%80%BCI/)哦~
184+
>
185+
> 千篇源码题解[已开源](https://github.com/LetMeFly666/LeetCode)

Solutions/Other-English-LearningNotes-SomeWords.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,6 +1209,9 @@ categories: [自用]
12091209
|plume|n. 羽毛,飘升之物,一缕,(常用作装饰的)连在一起的羽毛<br/>v. 用羽毛装饰,(鸟)整理(羽毛),借衣服修饰,自夸|
12101210
|||
12111211
|indefinitely|adv. 无期限的|
1212+
|||
1213+
|prayer|n. 祷告,祈祷文,经文|
1214+
|better-off|adj. 比较富裕的,境况较好的|
12121215

12131216
<p class="wordCounts">单词收录总数</p>
12141217

Solutions/Other-Japanese-LearningNotes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -800,6 +800,7 @@ xx型
800800
|お兄さん(おにいさん)|哥哥|
801801
|姉妹(しまい)|姐妹|
802802
|兄弟(きょうだい)|兄弟|
803+
|従兄弟/従姉妹(いとこ)|堂表兄弟姐妹|
803804
|かれ||
804805
|たち||
805806
|だれ||
File renamed without changes.

0 commit comments

Comments
 (0)