Skip to content

Commit 09de65f

Browse files
committed
Sync LeetCode submission Runtime - 1 ms (100.00%), Memory - 17.8 MB (100.00%)
1 parent 9b8522a commit 09de65f

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<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>
2+
3+
<ul>
4+
<li>One of the characters has an <strong>even frequency</strong> in the string.</li>
5+
<li>The other character has an <strong>odd frequency</strong> in the string.</li>
6+
</ul>
7+
8+
<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>
9+
10+
<p>&nbsp;</p>
11+
<p><strong class="example">Example 1:</strong></p>
12+
13+
<div class="example-block">
14+
<p><strong>Input:</strong> <span class="example-io">s = &quot;aaaaabbc&quot;</span></p>
15+
16+
<p><strong>Output:</strong> 3</p>
17+
18+
<p><strong>Explanation:</strong></p>
19+
20+
<ul>
21+
<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>
22+
<li>The maximum difference is <code>5 - 2 = 3</code>.</li>
23+
</ul>
24+
</div>
25+
26+
<p><strong class="example">Example 2:</strong></p>
27+
28+
<div class="example-block">
29+
<p><strong>Input:</strong> <span class="example-io">s = &quot;abcabcab&quot;</span></p>
30+
31+
<p><strong>Output:</strong> 1</p>
32+
33+
<p><strong>Explanation:</strong></p>
34+
35+
<ul>
36+
<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>
37+
<li>The maximum difference is <code>3 - 2 = 1</code>.</li>
38+
</ul>
39+
</div>
40+
41+
<p>&nbsp;</p>
42+
<p><strong>Constraints:</strong></p>
43+
44+
<ul>
45+
<li><code>3 &lt;= s.length &lt;= 100</code></li>
46+
<li><code>s</code> consists only of lowercase English letters.</li>
47+
<li><code>s</code> contains at least one character with an odd frequency and one with an even frequency.</li>
48+
</ul>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Time: O(n)
2+
# Space: O(k), k = number of unique chars in string
3+
4+
from collections import Counter
5+
6+
class Solution:
7+
def maxDifference(self, s: str) -> int:
8+
freq = Counter(s)
9+
10+
max_odd = float('-inf')
11+
min_even = float('inf')
12+
13+
for count in freq.values():
14+
if count % 2 == 1:
15+
max_odd = max(max_odd, count)
16+
else:
17+
min_even = min(min_even, count)
18+
19+
return max_odd - min_even
20+

0 commit comments

Comments
 (0)