2272. Substring With Largest Variance LeetCode Solution

In this guide, you will get 2272. Substring With Largest Variance LeetCode Solution with the best time and space complexity. The solution to Substring With Largest Variance problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Substring With Largest Variance solution in C++
  4. Substring With Largest Variance solution in Java
  5. Substring With Largest Variance solution in Python
  6. Additional Resources
2272. Substring With Largest Variance LeetCode Solution image

Problem Statement of 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:

Input: s = “aababbb”
Output: 3
Explanation:
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.

Example 2:

Input: s = “abcde”
Output: 0
Explanation:
No letter occurs more than once in s, so the variance of every substring is 0.

Constraints:

1 <= s.length <= 104
s consists of lowercase English letters.

Complexity Analysis

  • Time Complexity: O(26^2n) = O(n)
  • Space Complexity: O(1)

2272. Substring With Largest Variance LeetCode Solution in C++

class Solution {
 public:
  int largestVariance(string s) {
    int ans = 0;

    for (char a = 'a'; a <= 'z'; ++a)
      for (char b = 'a'; b <= 'z'; ++b)
        if (a != b)
          ans = max(ans, kadane(s, a, b));

    return ans;
  }

 private:
  // a := the letter with the higher frequency
  // b := the letter with the lower frequency
  int kadane(const string& s, char a, char b) {
    int ans = 0;
    int countA = 0;
    int countB = 0;
    bool canExtendPrevB = false;

    for (const char c : s) {
      if (c != a && c != b)
        continue;
      if (c == a)
        ++countA;
      else
        ++countB;
      if (countB > 0) {
        // An interval should contain at least one b.
        ans = max(ans, countA - countB);
      } else if (countB == 0 && canExtendPrevB) {
        // edge case: consider the previous b.
        ans = max(ans, countA - 1);
      }
      // Reset if the number of b > the number of a.
      if (countB > countA) {
        countA = 0;
        countB = 0;
        canExtendPrevB = true;
      }
    }

    return ans;
  }
};
/* code provided by PROGIEZ */

2272. Substring With Largest Variance LeetCode Solution in Java

class Solution {
  public int largestVariance(String s) {
    int ans = 0;

    for (char a = 'a'; a <= 'z'; ++a)
      for (char b = 'a'; b <= 'z'; ++b)
        if (a != b)
          ans = Math.max(ans, kadane(s, a, b));

    return ans;
  }

  // a := the letter with the higher frequency
  // b := the letter with the lower frequency
  private int kadane(final String s, char a, char b) {
    int ans = 0;
    int countA = 0;
    int countB = 0;
    boolean canExtendPrevB = false;

    for (final char c : s.toCharArray()) {
      if (c != a && c != b)
        continue;
      if (c == a)
        ++countA;
      else
        ++countB;
      if (countB > 0) {
        // An interval should contain at least one b.
        ans = Math.max(ans, countA - countB);
      } else if (countB == 0 && canExtendPrevB) {
        // edge case: consider the previous b
        ans = Math.max(ans, countA - 1);
      }
      // Reset if the number of b > the number of a.
      if (countB > countA) {
        countA = 0;
        countB = 0;
        canExtendPrevB = true;
      }
    }

    return ans;
  }
}
// code provided by PROGIEZ

2272. Substring With Largest Variance LeetCode Solution in Python

class Solution:
  def largestVariance(self, s: str) -> int:
    # a := the letter with the higher frequency
    # b := the letter with the lower frequency
    def kadane(a: str, b: str) -> int:
      ans = 0
      countA = 0
      countB = 0
      canExtendPrevB = False

      for c in s:
        if c != a and c != b:
          continue
        if c == a:
          countA += 1
        else:
          countB += 1
        if countB > 0:
          # An interval should contain at least one b.
          ans = max(ans, countA - countB)
        elif countB == 0 and canExtendPrevB:
          # edge case: consider the previous b.
          ans = max(ans, countA - 1)
        # Reset if the number of b > the number of a.
        if countB > countA:
          countA = 0
          countB = 0
          canExtendPrevB = True

      return ans

    return max(kadane(a, b)
               for a in string.ascii_lowercase
               for b in string.ascii_lowercase
               if a != b)
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.