int characterReplacement(string s, int k) {
int best = 0;
for (int start = 0; start < (int)s.size(); start++) {
unordered_map<char, int> count;
int maxCount = 0;
for (int end = start; end < (int)s.size(); end++) {
count[s[end]]++;
maxCount = max(maxCount, count[s[end]]);
if (end - start + 1 - maxCount > k) break;
best = max(best, end - start + 1);
}
}
return best;
}
public int characterReplacement(String s, int k) {
int best = 0;
for (int start = 0; start < s.length(); start++) {
Map<Character, Integer> count = new HashMap<>();
int maxCount = 0;
for (int end = start; end < s.length(); end++) {
count.merge(s.charAt(end), 1, Integer::sum);
maxCount = Math.max(maxCount, count.get(s.charAt(end)));
if (end - start + 1 - maxCount > k) break;
best = Math.max(best, end - start + 1);
}
}
return best;
}
def character_replacement(s: str, k: int) -> int:
best = 0
for start in range(len(s)):
count = {}
max_count = 0
for end in range(start, len(s)):
count[s[end]] = count.get(s[end], 0) + 1
max_count = max(max_count, count[s[end]])
if end - start + 1 - max_count > k:
break
best = max(best, end - start + 1)
return best
function characterReplacement(s: string, k: number): number {
let best = 0;
for (let start = 0; start < s.length; start++) {
const count = new Map<string, number>();
let maxCount = 0;
for (let end = start; end < s.length; end++) {
count.set(s[end], (count.get(s[end]) ?? 0) + 1);
maxCount = Math.max(maxCount, count.get(s[end])!);
if (end - start + 1 - maxCount > k) break;
best = Math.max(best, end - start + 1);
}
}
return best;
}
int characterReplacement(string s, int k) {
unordered_map<char, int> count;
auto maxCount = [&] { int m = 0; for (auto& [c, v] : count) m = max(m, v); return m; };
int left = 0;
int best = 0;
for (int right = 0; right < (int)s.size(); right++) {
count[s[right]]++;
while (right - left + 1 - maxCount() > k) {
count[s[left]]--;
left++;
}
best = max(best, right - left + 1);
}
return best;
}
public int characterReplacement(String s, int k) {
Map<Character, Integer> count = new HashMap<>();
int left = 0;
int best = 0;
for (int right = 0; right < s.length(); right++) {
count.merge(s.charAt(right), 1, Integer::sum);
while (right - left + 1 - Collections.max(count.values()) > k) {
count.merge(s.charAt(left), -1, Integer::sum);
left++;
}
best = Math.max(best, right - left + 1);
}
return best;
}
def character_replacement(s: str, k: int) -> int:
count = {}
left = 0
best = 0
for right in range(len(s)):
count[s[right]] = count.get(s[right], 0) + 1
while right - left + 1 - max(count.values()) > k:
count[s[left]] -= 1
left += 1
best = max(best, right - left + 1)
return best
function characterReplacement(s: string, k: number): number {
const count = new Map<string, number>();
let left = 0;
let best = 0;
for (let right = 0; right < s.length; right++) {
count.set(s[right], (count.get(s[right]) ?? 0) + 1);
while (right - left + 1 - Math.max(...count.values()) > k) {
count.set(s[left], count.get(s[left])! - 1);
left++;
}
best = Math.max(best, right - left + 1);
}
return best;
}
int characterReplacement(string s, int k) {
unordered_map<char, int> count;
int left = 0;
int maxCount = 0;
int best = 0;
for (int right = 0; right < (int)s.size(); right++) {
count[s[right]]++;
maxCount = max(maxCount, count[s[right]]);
if (right - left + 1 - maxCount > k) {
count[s[left]]--;
left++;
}
best = max(best, right - left + 1);
}
return best;
}
public int characterReplacement(String s, int k) {
Map<Character, Integer> count = new HashMap<>();
int left = 0;
int maxCount = 0;
int best = 0;
for (int right = 0; right < s.length(); right++) {
count.merge(s.charAt(right), 1, Integer::sum);
maxCount = Math.max(maxCount, count.get(s.charAt(right)));
if (right - left + 1 - maxCount > k) {
count.merge(s.charAt(left), -1, Integer::sum);
left++;
}
best = Math.max(best, right - left + 1);
}
return best;
}
def character_replacement(s: str, k: int) -> int:
count = {}
left = 0
max_count = 0
best = 0
for right in range(len(s)):
count[s[right]] = count.get(s[right], 0) + 1
max_count = max(max_count, count[s[right]])
if right - left + 1 - max_count > k:
count[s[left]] -= 1
left += 1
best = max(best, right - left + 1)
return best
function characterReplacement(s: string, k: number): number {
const count = new Map<string, number>();
let left = 0;
let maxCount = 0;
let best = 0;
for (let right = 0; right < s.length; right++) {
count.set(s[right], (count.get(s[right]) ?? 0) + 1);
maxCount = Math.max(maxCount, count.get(s[right])!);
if (right - left + 1 - maxCount > k) {
count.set(s[left], count.get(s[left])! - 1);
left++;
}
best = Math.max(best, right - left + 1);
}
return best;
}
int characterReplacement(string s, int k) {
int n = s.size();
int count[26] = {0};
int left = 0;
int maxCount = 0;
for (int right = 0; right < n; right++) {
int c = s[right] - 'A';
maxCount = max(maxCount, ++count[c]);
if (right - left + 1 - maxCount > k) count[s[left++] - 'A']--;
}
return n - left;
}
public int characterReplacement(String s, int k) {
int n = s.length();
int[] count = new int[26];
int left = 0;
int maxCount = 0;
for (int right = 0; right < n; right++) {
int c = s.charAt(right) - 'A';
maxCount = Math.max(maxCount, ++count[c]);
if (right - left + 1 - maxCount > k) count[s.charAt(left++) - 'A']--;
}
return n - left;
}
def character_replacement(s: str, k: int) -> int:
n = len(s)
count = [0] * 26
left = 0
max_count = 0
for right in range(n):
c = ord(s[right]) - 65
count[c] += 1
max_count = max(max_count, count[c])
if right - left + 1 - max_count > k:
count[ord(s[left]) - 65] -= 1
left += 1
return n - left
function characterReplacement(s: string, k: number): number {
const n = s.length;
const count = new Array<number>(26).fill(0);
let left = 0;
let maxCount = 0;
for (let right = 0; right < n; right++) {
const c = s.charCodeAt(right) - 65;
maxCount = Math.max(maxCount, ++count[c]);
if (right - left + 1 - maxCount > k) count[s.charCodeAt(left++) - 65]--;
}
return n - left;
}