Substring With Largest Variance - Problem
The Variance Challenge: Finding Maximum Character Frequency Differences

You're given a string containing only lowercase English letters, and your mission is to find the largest possible variance among all its substrings.

What is variance? The variance of a string is defined as the largest difference between the number of occurrences of any 2 characters present in the string. These two characters can be the same or different.

For example, in the string "aaba", character 'a' appears 3 times and 'b' appears 1 time, so the variance is 3 - 1 = 2.

Your Goal: Among all possible contiguous substrings of the input string, find the one with the maximum variance and return that maximum variance value.

Input: A string s consisting of lowercase English letters only
Output: An integer representing the largest variance possible among all substrings

Input & Output

example_1.py โ€” Basic case
$ Input: s = "aababbb"
โ€บ Output: 3
๐Ÿ’ก Note: The substring "abbb" has the largest variance where 'b' appears 3 times and 'a' appears 1 time, giving variance = 3 - 1 = 2. However, the substring "aababbb" has 'b' appearing 4 times and 'a' appearing 3 times, giving variance = 4 - 3 = 1. Actually, "bbb" has 'b' appearing 3 times with no other characters, but we need at least 2 different characters. The optimal is "abbb" or "bbba" with variance 2. Wait, let me recalculate: "ababbb" has a=2, b=4, variance=2. "aababbb" has a=3, b=4, variance=1. The answer should be 3 from substring "bbb" if we consider implicit characters, but since we need 2 different chars, it's "abbb" with variance 2. Actually, "aababbb" gives us variance of 4-3=1, "ababbb" gives 4-2=2, "babbb" gives 4-1=3. So the answer is 3.
example_2.py โ€” Single character
$ Input: s = "abcde"
โ€บ Output: 0
๐Ÿ’ก Note: Every character appears exactly once, so in any substring containing at least 2 different characters, the maximum frequency is 1 and minimum frequency is 1, giving variance = 1 - 1 = 0.
example_3.py โ€” Two characters
$ Input: s = "aaaa"
โ€บ Output: 0
๐Ÿ’ก Note: The string contains only one unique character 'a', so we cannot form any substring with at least 2 different characters. Therefore, the variance is 0.

Constraints

  • 1 โ‰ค s.length โ‰ค 104
  • s consists of lowercase English letters only
  • The variance is calculated only for substrings with at least 2 different characters

Visualization

Tap to expand
๐ŸŽฏ Stock Portfolio Variance AnalysisPortfolio Timeline: "aabbaaa"Comparing Stock A (winner) vs Stock B (loser)A+1A+1B-1B-1A+1A+1A+1Kadane's Algorithm - Running Net Gain:1210123Best Time Period: Last 3 days ("aaa") but needs both stocks!Actual Best: Days 1-7 ("aabbaaa") = 5A - 2B = Net gain 3AABBAAAPortfolio Analysis: Stock A appears 5 times, Stock B appears 2 timesMaximum Variance = 5 - 2 = 3๐Ÿ”„ Repeat this analysis for all possible stock pairs (A,B), (A,C), (B,A), etc.โšก Time Complexity: O(26ยฒ ร— n) = O(n) since we have at most 676 pairs
Understanding the Visualization
1
๐Ÿ“ˆ Choose Stock Pair
Select two stocks (characters) to compare - one as 'winner' (+1) and one as 'loser' (-1)
2
๐Ÿ“Š Transform Timeline
Convert the timeline: winner stock days = +1, loser stock days = -1, others = 0
3
๐Ÿ” Find Best Period
Use Kadane's algorithm to find the time period with maximum net gain
4
๐Ÿ† Compare All Pairs
Repeat for all possible stock pairs and take the maximum variance
Key Takeaway
๐ŸŽฏ Key Insight: By transforming the variance problem into a maximum subarray problem using Kadane's algorithm, we can efficiently find the optimal substring in linear time for each character pair!
Asked in
Google 45 Meta 32 Amazon 28 Microsoft 15
38.4K Views
Medium-High Frequency
~25 min Avg. Time
1.2K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen