Substring With Largest 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. Note the two characters may or may not be the same.

Given a string s consisting of lowercase English letters only, return the largest variance possible among all substrings of s.

A substring is a contiguous sequence of characters within a string.

Example 1
Inputs = "aababbb"
Output3
Since the largest possible variance is 3, we return it.
Example 2
Inputs = "abcde"
Output0
No letter occurs more than once in s, so the variance of every substring is 0.

Constraints

  • 1 <= s.length <= 10^4
  • s consists of lowercase English letters.

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate