longest-repeating-character-replacement py3

This commit is contained in:
Sangeeth Sudheer 2022-04-24 16:50:53 +05:30
parent b904a8bb91
commit 2a7ce3a29f
3 changed files with 111 additions and 0 deletions

View File

@ -0,0 +1,26 @@
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 <= 105`
* `s` consists of only uppercase English letters.
* `0 <= k <= s.length`
https://leetcode.com/problems/longest-repeating-character-replacement

View File

@ -0,0 +1,42 @@
# Time: O(N^2)
# Space: O(N)
from collections import Counter
class Solution:
def characterReplacement(self, s: str, k: int) -> int:
'''
Sliding window approach
'''
def window_size(l, r):
return r - l + 1
# Need to keep track of frequency of each character in the
# window
count = Counter()
l = r = 0
result = 0
while r < len(s):
# Increment the frequency of the `r`th char
count.update(s[r])
# We can perform upto K replacements. So we prefer least num
# of replacements (<= K). In the current window, we can perform
# least replacements for the most common character in the range.
#
# e.g. AAABB K=2, in range 0 to 4, most common char is A, count=3
# so it just needs 5 - 3 = 2 replacements which is <= K which is ok.
#
# But, if K = 1, then we need 5 - 2 = 2 replacements for the same range
# but 2 > K so this window is not valid. We need to move left pointer in
# this case and reduce its count.
if window_size(l, r) - count.most_common(1)[0][1] > k:
count.subtract(s[l])
l += 1
result = max(result, window_size(l, r))
r += 1
return result

View File

@ -0,0 +1,43 @@
# Time: O(N)
# Space: O(N)
from collections import Counter
class Solution:
def characterReplacement(self, s: str, k: int) -> int:
'''
Sliding window approach, optimized
'''
def window_size(l, r):
return r - l + 1
# Need to keep track of frequency of each character in the
# window
count = Counter()
# We can optimize the prev sliding window approach by just keeping
# track of maximum frequency/count of an element we've seen so far.
maxf = 0
l = r = 0
result = 0
while r < len(s):
# Increment the frequency of the `r`th char
count.update(s[r])
# If right's frequency is bigger, make it the new
# max frequency
maxf = max(maxf, count[s[r]])
# Refer to neetcode: https://www.youtube.com/watch?v=gqXU1UyA8pk
# TODO: Document this approach my own way
if window_size(l, r) - maxf > k:
count.subtract(s[l])
l += 1
result = max(result, window_size(l, r))
r += 1
return result