mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-10 18:48:13 +08:00
38 lines
1.8 KiB
HTML
38 lines
1.8 KiB
HTML
<p>The <strong>variance</strong> of a string is defined as the largest difference between the number of occurrences of <strong>any</strong> <code>2</code> characters present in the string. Note the two characters may or may not be the same.</p>
|
|
|
|
<p>Given a string <code>s</code> consisting of lowercase English letters only, return <em>the <strong>largest variance</strong> possible among all <strong>substrings</strong> of</em> <code>s</code>.</p>
|
|
|
|
<p>A <strong>substring</strong> is a contiguous sequence of characters within a string.</p>
|
|
|
|
<p> </p>
|
|
<p><strong class="example">Example 1:</strong></p>
|
|
|
|
<pre>
|
|
<strong>Input:</strong> s = "aababbb"
|
|
<strong>Output:</strong> 3
|
|
<strong>Explanation:</strong>
|
|
All possible variances along with their respective substrings are listed below:
|
|
- Variance 0 for substrings "a", "aa", "ab", "abab", "aababb", "ba", "b", "bb", and "bbb".
|
|
- Variance 1 for substrings "aab", "aba", "abb", "aabab", "ababb", "aababbb", and "bab".
|
|
- Variance 2 for substrings "aaba", "ababbb", "abbb", and "babb".
|
|
- Variance 3 for substring "babbb".
|
|
Since the largest possible variance is 3, we return it.
|
|
</pre>
|
|
|
|
<p><strong class="example">Example 2:</strong></p>
|
|
|
|
<pre>
|
|
<strong>Input:</strong> s = "abcde"
|
|
<strong>Output:</strong> 0
|
|
<strong>Explanation:</strong>
|
|
No letter occurs more than once in s, so the variance of every substring is 0.
|
|
</pre>
|
|
|
|
<p> </p>
|
|
<p><strong>Constraints:</strong></p>
|
|
|
|
<ul>
|
|
<li><code>1 <= s.length <= 10<sup>4</sup></code></li>
|
|
<li><code>s</code> consists of lowercase English letters.</li>
|
|
</ul>
|