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
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
Output: An integer representing the largest variance possible among all substrings
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 onlyOutput: 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
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!
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code