Загрузка...

Maximum Difference Between Even and Odd Frequency II - Leetcode 3445

Quick explanation:
We explore every pair (a, b) to maximize freq[a] - freq[b].

We sliding-window the string of length ≥ k, keeping track of prefix counts and parity states.

For each valid window, the difference between current (cntA – cntB) and the smallest earlier (prevA – prevB) with flipped a-parity gives a candidate result.

We keep the global maximum across all pairs and windows.
This efficiently finds the maximum difference where a appears an odd number of times and b an even number, across all substrings of length at least k.

For the complexities:

Time Complexity: We have 25 pairs of (a, b), and each pair uses a sliding window across the string once, so O(25·n) = O(n)

Space Complexity: O(1) extra space (just counters, arrays of size 4, etc.)

Видео Maximum Difference Between Even and Odd Frequency II - Leetcode 3445 канала ThinkOutsideTheBox
Яндекс.Метрика

На информационно-развлекательном портале SALDA.WS применяются cookie-файлы. Нажимая кнопку Принять, вы подтверждаете свое согласие на их использование.

Об использовании CookiesПринять