2272. Substring With Largest Variance

Updated: 2024-03-12
1 min read
[]

On This Page

LeetCode problem 2272

class Solution:
    def largestVariance(self, s: str) -> int:
        res = 0
        for a, b in permutations(ascii_lowercase, 2):
            if a == b:
                continue
            f = [0, -inf]
            for c in s:
                if c == a:
                    f[0], f[1] = f[0] + 1, f[1] + 1
                elif c == b:
                    f[1] = max(f[1] - 1, f[0] - 1)
                    f[0] = 0
                if res < f[1]:
                    res = f[1]
        return res