Medium
You are given a string s and an integer k.
You can choose any character of the string and change it to any other
uppercase English character. You can perform this operation at most k times.
Return the length of the longest substring containing the same letter you can get after performing the above operations.
Example 1:
Input: s = "ABAB", k = 2 Output: 4 Explanation: Replace the two 'A's with two 'B's or vice versa.
Example 2:
Input: s = "AABABBA", k = 1 Output: 4 Explanation: Replace the one 'A' in the middle with 'B' and form "AABBBBA". The substring "BBBB" has the longest repeating letters, which is 4.
Constraints:
1 <= s.length <= 105sconsists of only uppercase English letters.0 <= k <= s.length
/*
Given a string s & an int k, can change any char k times:
Return length of longest substring containing same letter
Ex. s = "ABAB" k = 2 -> 4 "AAAA", s = "AABABBA" k = 1 -> 4
Sliding window, expand if can change char, contract if > k
Time: O(n)
Space: O(26)
*/
class Solution {
public:
int characterReplacement(string s, int k) {
vector<int> count(26);
int maxCount = 0;
int i = 0;
int j = 0;
int result = 0;
while (j < s.size()) {
count[s[j] - 'A']++;
maxCount = max(maxCount, count[s[j] - 'A']);
if (j - i + 1 - maxCount > k) {
count[s[i] - 'A']--;
i++;
}
result = max(result, j - i + 1);
j++;
}
return result;
}
};
No comments:
Post a Comment