Home
Log in
DescriptionSubmissionsSolution
DescriptionSubmissionsSolution
Loading...

Log in to run or submit

s =
k =

Codey

Practise coding problems, test your solutions and track your progress.

Explore

  • Problems
  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

© 2026 Codey. Personal learning project.

Solution
Updated: 2026-02-23

Idea

Use a sliding window and keep track of the most frequent character count in the window.

Approach

Expand the window to the right, updating counts and max_freq. If the window needs more than k replacements (window_len - max_freq > k), shrink from the left.

Why it works

A window is valid if you can replace all non-majority characters within k. Tracking max_freq lets you check validity in O(1).

Code
Loading...
Complexity
Time: O(n)
Space: O(1)
Solution
Updated: 2026-02-23

Idea

Use a sliding window and keep track of the most frequent character count in the window.

Approach

Expand the window to the right, updating counts and max_freq. If the window needs more than k replacements (window_len - max_freq > k), shrink from the left.

Why it works

A window is valid if you can replace all non-majority characters within k. Tracking max_freq lets you check validity in O(1).

Code
Loading...
Complexity
Time: O(n)
Space: O(1)