Count Longest Substrings After Character Replacements
Company: Pinduoduo
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Technical Screen
Overview: Count the longest substring occurrences that can become one repeated uppercase letter within a replacement budget, counting positions rather than replacement choices.
Read the full Pinduoduo Software Engineer interview experience this question came from
Constraints
- s contains uppercase English letters, 1 <= s.length <= 100000, and 0 <= k <= s.length.
- A nonempty occurrence is valid when at most k replacements within that occurrence can make it uniform.
- First determine the greatest valid length, then count only valid occurrences of exactly that length by their original positions.
- Overlapping occurrences and identical text at different positions count separately; replacement choices or target letters never multiply a single occurrence.
- Each occurrence is evaluated independently on the original string. At k=0 it must already be uniform; a one-character input returns one.
Examples
Input: ('AABABBA', 1)
Expected Output: 2
Explanation: Published sample 1: only AABA and BABB are valid among the length-four windows, and no length-five window qualifies.
Input: ('ABAB', 2)
Expected Output: 1
Explanation: Published sample 2: the entire string is one occurrence even though either A or B can be the replacement target.