-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add solution for Longest Repeating Character Replacement
- Loading branch information
1 parent
5dac994
commit 1527e3e
Showing
1 changed file
with
19 additions
and
0 deletions.
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
camp/week1/leetcode/longest-repeating-character-replacement.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
class Solution: | ||
def characterReplacement(self, s: str, k: int) -> int: | ||
left=0 | ||
right=0 | ||
dic={} | ||
_max=0 | ||
while right <len(s): | ||
dic[s[right]]=dic.get(s[right],0) +1 | ||
window=right-left+1 | ||
if window - max(dic.values()) <= k: | ||
_max=max(_max,sum(dic.values())) | ||
else: | ||
dic[s[left]]-= 1 | ||
if not dic[s[left]]: | ||
del dic[s[left]] | ||
left+=1 | ||
right+=1 | ||
return _max | ||
|