|
| 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> </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 = "12233", 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>"12233"</code>, the frequency of <code>'1'</code> is 1 and the frequency of <code>'3'</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 = "1122211", 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>"11222"</code>, the frequency of <code>'2'</code> is 3 and the frequency of <code>'1'</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 = "110", k = 3</span></p> |
| 42 | + |
| 43 | +<p><strong>Output:</strong> <span class="example-io">-1</span></p> |
| 44 | +</div> |
| 45 | + |
| 46 | +<p> </p> |
| 47 | +<p><strong>Constraints:</strong></p> |
| 48 | + |
| 49 | +<ul> |
| 50 | + <li><code>3 <= s.length <= 3 * 10<sup>4</sup></code></li> |
| 51 | + <li><code>s</code> consists only of digits <code>'0'</code> to <code>'4'</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 <= k <= s.length</code></li> |
| 54 | +</ul> |
0 commit comments