Classic sliding window once you reframe it: you're just looking for the longest window with at most PTO occurrences of 'W'.
Use a sliding window (two-pointer) technique to maintain a window with at most PTO 'W' characters, expanding the right pointer and shrinking the left when the count exceeds PTO. Track the maximum window length seen. This yields an O(n) time and O(1) space solution.
Pro tip: Clarify edge cases upfront (e.g., PTO >= total W's, empty string) and mention that the sliding window works because the constraint is monotonic: adding characters can only increase W count, so shrinking is safe.
Restate the problem: find the longest contiguous substring where the number of 'W' characters is ≤ PTO. Confirm input/output and edge cases.
Select sliding window because it efficiently finds the longest subarray satisfying a monotonic constraint. Explain why brute force is O(n^2) and less optimal.
Initialize left=0, w_count=0, max_len=0. Iterate right from 0 to n-1: if s[right]=='W', increment w_count. While w_count > PTO, if s[left]=='W', decrement w_count; increment left. Update max_len = max(max_len, right-left+1).
Time complexity O(n) because each character is visited at most twice (by right and left pointers). Space complexity O(1) as only a few variables are used.
Walk through a small example (e.g., 'HWWWH', PTO=1) to verify correctness. Also test edge cases: all 'H', all 'W', PTO=0, PTO >= total W's.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.