Skip to content

Commit c905bfa

Browse files
authored
feat: add weekly contest 435 (#4015)
1 parent d1eddfa commit c905bfa

File tree

23 files changed

+1600
-1
lines changed

23 files changed

+1600
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
---
2+
comments: true
3+
difficulty: 简单
4+
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3442.Maximum%20Difference%20Between%20Even%20and%20Odd%20Frequency%20I/README.md
5+
---
6+
7+
<!-- problem:start -->
8+
9+
# [3442. 奇偶频次间的最大差值 I](https://leetcode.cn/problems/maximum-difference-between-even-and-odd-frequency-i)
10+
11+
[English Version](/solution/3400-3499/3442.Maximum%20Difference%20Between%20Even%20and%20Odd%20Frequency%20I/README_EN.md)
12+
13+
## 题目描述
14+
15+
<!-- description:start -->
16+
17+
<p>给你一个由小写英文字母组成的字符串&nbsp;<code>s</code> 。请你找出字符串中两个字符的出现频次之间的 <strong>最大</strong> 差值,这两个字符需要满足:</p>
18+
19+
<ul>
20+
<li>一个字符在字符串中出现 <strong>偶数次</strong> 。</li>
21+
<li>另一个字符在字符串中出现 <strong>奇数次</strong>&nbsp;。</li>
22+
</ul>
23+
24+
<p>返回 <strong>最大</strong> 差值,计算方法是出现 <strong>奇数次</strong> 字符的次数 <strong>减去</strong> 出现 <strong>偶数次</strong> 字符的次数。</p>
25+
26+
<p>&nbsp;</p>
27+
28+
<p><b>示例 1:</b></p>
29+
30+
<div class="example-block">
31+
<p><span class="example-io"><b>输入:</b>s = "aaaaabbc"</span></p>
32+
33+
<p><b>输出:</b>3</p>
34+
35+
<p><b>解释:</b></p>
36+
37+
<ul>
38+
<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>
39+
<li>最大差值为&nbsp;<code>5 - 2 = 3</code>&nbsp;。</li>
40+
</ul>
41+
</div>
42+
43+
<p><b>示例 2:</b></p>
44+
45+
<div class="example-block">
46+
<p><span class="example-io"><b>输入:</b>s = "abcabcab"</span></p>
47+
48+
<p><b>输出:</b>1</p>
49+
50+
<p><b>解释:</b></p>
51+
52+
<ul>
53+
<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>
54+
<li>最大差值为&nbsp;<code>3 - 2 = 1</code> 。</li>
55+
</ul>
56+
</div>
57+
58+
<p>&nbsp;</p>
59+
60+
<p><b>提示:</b></p>
61+
62+
<ul>
63+
<li><code>3 &lt;= s.length &lt;= 100</code></li>
64+
<li><code>s</code>&nbsp;仅由小写英文字母组成。</li>
65+
<li><code>s</code>&nbsp;至少由一个出现奇数次的字符和一个出现偶数次的字符组成。</li>
66+
</ul>
67+
68+
<!-- description:end -->
69+
70+
## 解法
71+
72+
<!-- solution:start -->
73+
74+
### 方法一:计数
75+
76+
我们可以用一个哈希表或数组 $\textit{cnt}$ 记录字符串 $s$ 中每个字符的出现次数。然后遍历 $\textit{cnt}$,找出出现奇数次的字符的最大频次 $a$ 和出现偶数次的字符的最小频次 $b$,最后返回 $a - b$ 即可。
77+
78+
时间复杂度 $O(n)$,其中 $n$ 是字符串 $s$ 的长度。空间复杂度 $O(|\Sigma|)$,其中 $\Sigma$ 是字符集,本题中 $|\Sigma| = 26$。
79+
80+
<!-- tabs:start -->
81+
82+
#### Python3
83+
84+
```python
85+
class Solution:
86+
def maxDifference(self, s: str) -> int:
87+
cnt = Counter(s)
88+
a, b = 0, inf
89+
for v in cnt.values():
90+
if v % 2:
91+
a = max(a, v)
92+
else:
93+
b = min(b, v)
94+
return a - b
95+
```
96+
97+
#### Java
98+
99+
```java
100+
class Solution {
101+
public int maxDifference(String s) {
102+
int[] cnt = new int[26];
103+
for (char c : s.toCharArray()) {
104+
++cnt[c - 'a'];
105+
}
106+
int a = 0, b = 1 << 30;
107+
for (int v : cnt) {
108+
if (v % 2 == 1) {
109+
a = Math.max(a, v);
110+
} else if (v > 0) {
111+
b = Math.min(b, v);
112+
}
113+
}
114+
return a - b;
115+
}
116+
}
117+
```
118+
119+
#### C++
120+
121+
```cpp
122+
class Solution {
123+
public:
124+
int maxDifference(string s) {
125+
int cnt[26]{};
126+
for (char c : s) {
127+
++cnt[c - 'a'];
128+
}
129+
int a = 0, b = 1 << 30;
130+
for (int v : cnt) {
131+
if (v % 2 == 1) {
132+
a = max(a, v);
133+
} else if (v > 0) {
134+
b = min(b, v);
135+
}
136+
}
137+
return a - b;
138+
}
139+
};
140+
```
141+
142+
#### Go
143+
144+
```go
145+
func maxDifference(s string) int {
146+
cnt := [26]int{}
147+
for _, c := range s {
148+
cnt[c-'a']++
149+
}
150+
a, b := 0, 1<<30
151+
for _, v := range cnt {
152+
if v%2 == 1 {
153+
a = max(a, v)
154+
} else if v > 0 {
155+
b = min(b, v)
156+
}
157+
}
158+
return a - b
159+
}
160+
```
161+
162+
#### TypeScript
163+
164+
```ts
165+
function maxDifference(s: string): number {
166+
const cnt: Record<string, number> = {};
167+
for (const c of s) {
168+
cnt[c] = (cnt[c] || 0) + 1;
169+
}
170+
let [a, b] = [0, Infinity];
171+
for (const [_, v] of Object.entries(cnt)) {
172+
if (v % 2 === 1) {
173+
a = Math.max(a, v);
174+
} else {
175+
b = Math.min(b, v);
176+
}
177+
}
178+
return a - b;
179+
}
180+
```
181+
182+
<!-- tabs:end -->
183+
184+
<!-- solution:end -->
185+
186+
<!-- problem:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
---
2+
comments: true
3+
difficulty: Easy
4+
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3442.Maximum%20Difference%20Between%20Even%20and%20Odd%20Frequency%20I/README_EN.md
5+
---
6+
7+
<!-- problem:start -->
8+
9+
# [3442. Maximum Difference Between Even and Odd Frequency I](https://leetcode.com/problems/maximum-difference-between-even-and-odd-frequency-i)
10+
11+
[中文文档](/solution/3400-3499/3442.Maximum%20Difference%20Between%20Even%20and%20Odd%20Frequency%20I/README.md)
12+
13+
## Description
14+
15+
<!-- description:start -->
16+
17+
<p>You are given a string <code>s</code> consisting of lowercase English letters. Your task is to find the <strong>maximum</strong> difference between the frequency of <strong>two</strong> characters in the string such that:</p>
18+
19+
<ul>
20+
<li>One of the characters has an <strong>even frequency</strong> in the string.</li>
21+
<li>The other character has an <strong>odd frequency</strong> in the string.</li>
22+
</ul>
23+
24+
<p>Return the <strong>maximum</strong> difference, calculated as the frequency of the character with an <b>odd</b> frequency <strong>minus</strong> the frequency of the character with an <b>even</b> frequency.</p>
25+
26+
<p>&nbsp;</p>
27+
<p><strong class="example">Example 1:</strong></p>
28+
29+
<div class="example-block">
30+
<p><strong>Input:</strong> <span class="example-io">s = &quot;aaaaabbc&quot;</span></p>
31+
32+
<p><strong>Output:</strong> 3</p>
33+
34+
<p><strong>Explanation:</strong></p>
35+
36+
<ul>
37+
<li>The character <code>&#39;a&#39;</code> has an <strong>odd frequency</strong> of <code><font face="monospace">5</font></code><font face="monospace">,</font> and <code>&#39;b&#39;</code> has an <strong>even frequency</strong> of <code><font face="monospace">2</font></code>.</li>
38+
<li>The maximum difference is <code>5 - 2 = 3</code>.</li>
39+
</ul>
40+
</div>
41+
42+
<p><strong class="example">Example 2:</strong></p>
43+
44+
<div class="example-block">
45+
<p><strong>Input:</strong> <span class="example-io">s = &quot;abcabcab&quot;</span></p>
46+
47+
<p><strong>Output:</strong> 1</p>
48+
49+
<p><strong>Explanation:</strong></p>
50+
51+
<ul>
52+
<li>The character <code>&#39;a&#39;</code> has an <strong>odd frequency</strong> of <code><font face="monospace">3</font></code><font face="monospace">,</font> and <code>&#39;c&#39;</code> has an <strong>even frequency</strong> of <font face="monospace">2</font>.</li>
53+
<li>The maximum difference is <code>3 - 2 = 1</code>.</li>
54+
</ul>
55+
</div>
56+
57+
<p>&nbsp;</p>
58+
<p><strong>Constraints:</strong></p>
59+
60+
<ul>
61+
<li><code>3 &lt;= s.length &lt;= 100</code></li>
62+
<li><code>s</code> consists only of lowercase English letters.</li>
63+
<li><code>s</code> contains at least one character with an odd frequency and one with an even frequency.</li>
64+
</ul>
65+
66+
<!-- description:end -->
67+
68+
## Solutions
69+
70+
<!-- solution:start -->
71+
72+
### Solution 1: Counting
73+
74+
We can use a hash table or an array $\textit{cnt}$ to record the occurrences of each character in the string $s$. Then, we traverse $\textit{cnt}$ to find the maximum frequency $a$ of characters that appear an odd number of times and the minimum frequency $b$ of characters that appear an even number of times. Finally, we return $a - b$.
75+
76+
The time complexity is $O(n)$, where $n$ is the length of the string $s$. The space complexity is $O(|\Sigma|)$, where $\Sigma$ is the character set. In this problem, $|\Sigma| = 26$.
77+
78+
<!-- tabs:start -->
79+
80+
#### Python3
81+
82+
```python
83+
class Solution:
84+
def maxDifference(self, s: str) -> int:
85+
cnt = Counter(s)
86+
a, b = 0, inf
87+
for v in cnt.values():
88+
if v % 2:
89+
a = max(a, v)
90+
else:
91+
b = min(b, v)
92+
return a - b
93+
```
94+
95+
#### Java
96+
97+
```java
98+
class Solution {
99+
public int maxDifference(String s) {
100+
int[] cnt = new int[26];
101+
for (char c : s.toCharArray()) {
102+
++cnt[c - 'a'];
103+
}
104+
int a = 0, b = 1 << 30;
105+
for (int v : cnt) {
106+
if (v % 2 == 1) {
107+
a = Math.max(a, v);
108+
} else if (v > 0) {
109+
b = Math.min(b, v);
110+
}
111+
}
112+
return a - b;
113+
}
114+
}
115+
```
116+
117+
#### C++
118+
119+
```cpp
120+
class Solution {
121+
public:
122+
int maxDifference(string s) {
123+
int cnt[26]{};
124+
for (char c : s) {
125+
++cnt[c - 'a'];
126+
}
127+
int a = 0, b = 1 << 30;
128+
for (int v : cnt) {
129+
if (v % 2 == 1) {
130+
a = max(a, v);
131+
} else if (v > 0) {
132+
b = min(b, v);
133+
}
134+
}
135+
return a - b;
136+
}
137+
};
138+
```
139+
140+
#### Go
141+
142+
```go
143+
func maxDifference(s string) int {
144+
cnt := [26]int{}
145+
for _, c := range s {
146+
cnt[c-'a']++
147+
}
148+
a, b := 0, 1<<30
149+
for _, v := range cnt {
150+
if v%2 == 1 {
151+
a = max(a, v)
152+
} else if v > 0 {
153+
b = min(b, v)
154+
}
155+
}
156+
return a - b
157+
}
158+
```
159+
160+
#### TypeScript
161+
162+
```ts
163+
function maxDifference(s: string): number {
164+
const cnt: Record<string, number> = {};
165+
for (const c of s) {
166+
cnt[c] = (cnt[c] || 0) + 1;
167+
}
168+
let [a, b] = [0, Infinity];
169+
for (const [_, v] of Object.entries(cnt)) {
170+
if (v % 2 === 1) {
171+
a = Math.max(a, v);
172+
} else {
173+
b = Math.min(b, v);
174+
}
175+
}
176+
return a - b;
177+
}
178+
```
179+
180+
<!-- tabs:end -->
181+
182+
<!-- solution:end -->
183+
184+
<!-- problem:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public:
3+
int maxDifference(string s) {
4+
int cnt[26]{};
5+
for (char c : s) {
6+
++cnt[c - 'a'];
7+
}
8+
int a = 0, b = 1 << 30;
9+
for (int v : cnt) {
10+
if (v % 2 == 1) {
11+
a = max(a, v);
12+
} else if (v > 0) {
13+
b = min(b, v);
14+
}
15+
}
16+
return a - b;
17+
}
18+
};

0 commit comments

Comments
 (0)