Leetcode Practice
LC 424. Longest Repeating Character Replacement
- Last updated
- Reading time
- 1 min read
The problem
First Solution
function characterReplacement(s: string, k: number): number {
let answer = 0
let freqHash = {}
let maxFreq = 0
let leftPointer = 0
for (let i = 0; i < s.length; i++) {
freqHash[s[i]] = freqHash[s[i]] ? freqHash[s[i]] + 1 : 1
maxFreq = Math.max(maxFreq, freqHash[s[i]])
if (i + 1 - leftPointer - maxFreq > k) {
freqHash[s[leftPointer]]--
leftPointer++
}
answer = Math.max(answer, i + 1 - leftPointer)
}
return answer
}