Skip to content

Commit 3dc8e7a

Browse files
committed
Sync LeetCode submission Runtime - 3046 ms (50.00%), Memory - 18.2 MB (94.83%)
1 parent 9659470 commit 3dc8e7a

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<p>You are given a string <code>s</code> and an integer <code>k</code>. Your task is to find the <strong>maximum</strong> difference between the frequency of <strong>two</strong> characters, <code>freq[a] - freq[b]</code>, in a <span data-keyword="substring">substring</span> <code>subs</code> of <code>s</code>, such that:</p>
2+
3+
<ul>
4+
<li><code>subs</code> has a size of <strong>at least</strong> <code>k</code>.</li>
5+
<li>Character <code>a</code> has an <em>odd frequency</em> in <code>subs</code>.</li>
6+
<li>Character <code>b</code> has an <em>even frequency</em> in <code>subs</code>.</li>
7+
</ul>
8+
9+
<p>Return the <strong>maximum</strong> difference.</p>
10+
11+
<p><strong>Note</strong> that <code>subs</code> can contain more than 2 <strong>distinct</strong> characters.</p>
12+
13+
<p>&nbsp;</p>
14+
<p><strong class="example">Example 1:</strong></p>
15+
16+
<div class="example-block">
17+
<p><strong>Input:</strong> <span class="example-io">s = &quot;12233&quot;, k = 4</span></p>
18+
19+
<p><strong>Output:</strong> <span class="example-io">-1</span></p>
20+
21+
<p><strong>Explanation:</strong></p>
22+
23+
<p>For the substring <code>&quot;12233&quot;</code>, the frequency of <code>&#39;1&#39;</code> is 1 and the frequency of <code>&#39;3&#39;</code> is 2. The difference is <code>1 - 2 = -1</code>.</p>
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;1122211&quot;, k = 3</span></p>
30+
31+
<p><strong>Output:</strong> <span class="example-io">1</span></p>
32+
33+
<p><strong>Explanation:</strong></p>
34+
35+
<p>For the substring <code>&quot;11222&quot;</code>, the frequency of <code>&#39;2&#39;</code> is 3 and the frequency of <code>&#39;1&#39;</code> is 2. The difference is <code>3 - 2 = 1</code>.</p>
36+
</div>
37+
38+
<p><strong class="example">Example 3:</strong></p>
39+
40+
<div class="example-block">
41+
<p><strong>Input:</strong> <span class="example-io">s = &quot;110&quot;, k = 3</span></p>
42+
43+
<p><strong>Output:</strong> <span class="example-io">-1</span></p>
44+
</div>
45+
46+
<p>&nbsp;</p>
47+
<p><strong>Constraints:</strong></p>
48+
49+
<ul>
50+
<li><code>3 &lt;= s.length &lt;= 3 * 10<sup>4</sup></code></li>
51+
<li><code>s</code> consists only of digits <code>&#39;0&#39;</code> to <code>&#39;4&#39;</code>.</li>
52+
<li>The input is generated that at least one substring has a character with an even frequency and a character with an odd frequency.</li>
53+
<li><code>1 &lt;= k &lt;= s.length</code></li>
54+
</ul>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class Solution:
2+
def maxDifference(self, s: str, k: int) -> int:
3+
def getStatus(cnt_a: int, cnt_b: int) -> int:
4+
return ((cnt_a & 1) << 1) | (cnt_b & 1)
5+
6+
n = len(s)
7+
ans = float("-inf")
8+
for a in ["0", "1", "2", "3", "4"]:
9+
for b in ["0", "1", "2", "3", "4"]:
10+
if a == b:
11+
continue
12+
13+
best = [float("inf")] * 4
14+
cnt_a = cnt_b = 0
15+
prev_a = prev_b = 0
16+
left = -1
17+
for right in range(n):
18+
cnt_a += s[right] == a
19+
cnt_b += s[right] == b
20+
while right - left >= k and cnt_b - prev_b >= 2:
21+
left_status = getStatus(prev_a, prev_b)
22+
best[left_status] = min(
23+
best[left_status], prev_a - prev_b
24+
)
25+
left += 1
26+
prev_a += s[left] == a
27+
prev_b += s[left] == b
28+
29+
right_status = getStatus(cnt_a, cnt_b)
30+
if best[right_status ^ 0b10] != float("inf"):
31+
ans = max(
32+
ans, cnt_a - cnt_b - best[right_status ^ 0b10]
33+
)
34+
35+
return ans

0 commit comments

Comments
 (0)