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
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
Комментарии отсутствуют
Информация о видео
11 июня 2025 г. 11:37:09
00:00:38
Другие видео канала