Skip to content

Commit b7603bb

Browse files
committed
feat: add weekly contest 435
1 parent d1eddfa commit b7603bb

File tree

24 files changed

+1593
-2
lines changed

24 files changed

+1593
-2
lines changed

Diff for: solution/2600-2699/2674.Split a Circular Linked List/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ tags:
4646
<p><strong>提示:</strong></p>
4747

4848
<ul>
49-
<li><code>list</code> 中的节点数范围为 <code>[2, 10<sup>5</sup>]</code></li>
49+
<li><code>list</code> 中的节点数范围为 <code>[2, 105]</code></li>
5050
<li><code>0 &lt;= Node.val &lt;= 10<sup>9</sup></code></li>
5151
<li><code>LastNode.next = FirstNode</code> ,其中 <code>LastNode</code> 是链表的最后一个节点,<code>FirstNode</code> 是第一个节点。</li>
5252
</ul>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
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+
<!-- tabs:start -->
77+
78+
#### Python3
79+
80+
```python
81+
class Solution:
82+
def maxDifference(self, s: str) -> int:
83+
cnt = Counter(s)
84+
a, b = 0, inf
85+
for v in cnt.values():
86+
if v % 2:
87+
a = max(a, v)
88+
else:
89+
b = min(b, v)
90+
return a - b
91+
```
92+
93+
#### Java
94+
95+
```java
96+
class Solution {
97+
public int maxDifference(String s) {
98+
int[] cnt = new int[26];
99+
for (char c : s.toCharArray()) {
100+
++cnt[c - 'a'];
101+
}
102+
int a = 0, b = 1 << 30;
103+
for (int v : cnt) {
104+
if (v % 2 == 1) {
105+
a = Math.max(a, v);
106+
} else if (v > 0) {
107+
b = Math.min(b, v);
108+
}
109+
}
110+
return a - b;
111+
}
112+
}
113+
```
114+
115+
#### C++
116+
117+
```cpp
118+
class Solution {
119+
public:
120+
int maxDifference(string s) {
121+
int cnt[26]{};
122+
for (char c : s) {
123+
++cnt[c - 'a'];
124+
}
125+
int a = 0, b = 1 << 30;
126+
for (int v : cnt) {
127+
if (v % 2 == 1) {
128+
a = max(a, v);
129+
} else if (v > 0) {
130+
b = min(b, v);
131+
}
132+
}
133+
return a - b;
134+
}
135+
};
136+
```
137+
138+
#### Go
139+
140+
```go
141+
func maxDifference(s string) int {
142+
cnt := [26]int{}
143+
for _, c := range s {
144+
cnt[c-'a']++
145+
}
146+
a, b := 0, 1<<30
147+
for _, v := range cnt {
148+
if v%2 == 1 {
149+
a = max(a, v)
150+
} else if v > 0 {
151+
b = min(b, v)
152+
}
153+
}
154+
return a - b
155+
}
156+
```
157+
158+
#### TypeScript
159+
160+
```ts
161+
function maxDifference(s: string): number {
162+
const cnt: Record<string, number> = {};
163+
for (const c of s) {
164+
cnt[c] = (cnt[c] || 0) + 1;
165+
}
166+
let [a, b] = [0, Infinity];
167+
for (const [_, v] of Object.entries(cnt)) {
168+
if (v % 2 === 1) {
169+
a = Math.max(a, v);
170+
} else {
171+
b = Math.min(b, v);
172+
}
173+
}
174+
return a - b;
175+
}
176+
```
177+
178+
<!-- tabs:end -->
179+
180+
<!-- solution:end -->
181+
182+
<!-- problem:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
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
73+
74+
<!-- tabs:start -->
75+
76+
#### Python3
77+
78+
```python
79+
class Solution:
80+
def maxDifference(self, s: str) -> int:
81+
cnt = Counter(s)
82+
a, b = 0, inf
83+
for v in cnt.values():
84+
if v % 2:
85+
a = max(a, v)
86+
else:
87+
b = min(b, v)
88+
return a - b
89+
```
90+
91+
#### Java
92+
93+
```java
94+
class Solution {
95+
public int maxDifference(String s) {
96+
int[] cnt = new int[26];
97+
for (char c : s.toCharArray()) {
98+
++cnt[c - 'a'];
99+
}
100+
int a = 0, b = 1 << 30;
101+
for (int v : cnt) {
102+
if (v % 2 == 1) {
103+
a = Math.max(a, v);
104+
} else if (v > 0) {
105+
b = Math.min(b, v);
106+
}
107+
}
108+
return a - b;
109+
}
110+
}
111+
```
112+
113+
#### C++
114+
115+
```cpp
116+
class Solution {
117+
public:
118+
int maxDifference(string s) {
119+
int cnt[26]{};
120+
for (char c : s) {
121+
++cnt[c - 'a'];
122+
}
123+
int a = 0, b = 1 << 30;
124+
for (int v : cnt) {
125+
if (v % 2 == 1) {
126+
a = max(a, v);
127+
} else if (v > 0) {
128+
b = min(b, v);
129+
}
130+
}
131+
return a - b;
132+
}
133+
};
134+
```
135+
136+
#### Go
137+
138+
```go
139+
func maxDifference(s string) int {
140+
cnt := [26]int{}
141+
for _, c := range s {
142+
cnt[c-'a']++
143+
}
144+
a, b := 0, 1<<30
145+
for _, v := range cnt {
146+
if v%2 == 1 {
147+
a = max(a, v)
148+
} else if v > 0 {
149+
b = min(b, v)
150+
}
151+
}
152+
return a - b
153+
}
154+
```
155+
156+
#### TypeScript
157+
158+
```ts
159+
function maxDifference(s: string): number {
160+
const cnt: Record<string, number> = {};
161+
for (const c of s) {
162+
cnt[c] = (cnt[c] || 0) + 1;
163+
}
164+
let [a, b] = [0, Infinity];
165+
for (const [_, v] of Object.entries(cnt)) {
166+
if (v % 2 === 1) {
167+
a = Math.max(a, v);
168+
} else {
169+
b = Math.min(b, v);
170+
}
171+
}
172+
return a - b;
173+
}
174+
```
175+
176+
<!-- tabs:end -->
177+
178+
<!-- solution:end -->
179+
180+
<!-- 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)