424 Longest Repeating Character Replacement
1. Question
Given a string that consists of only uppercase English letters, you can replace any letter in the string with another letter at mostktimes. Find the length of a longest substring containing all repeating letters you can get after performing the above operations.
Note: Both the string's length andkwill not exceed 104.
Example 1:
Example 2:
2. Implementation
(1) Two Pointer + Hash
思路: 这道题的本质是问找到一个最长的substring满足,该substring的长度减substring中最多重复的character的个数是小于等于k的。所以我们维护一个变量maxCount, 从左向右扫s, maxCount记录目前最多重复的character的个数,当substring的长度减去maxCount的值大于k时,我们需要更新substring的起点。
3. Time & Space Complexity
Two Pointer + Hash: 时间复杂度O(n), 空间复杂度O(1)
Previous395 Longest Substring with At Least K Repeating CharactersNext438 Find All Anagrams in a String
Last updated