From 2a7ce3a29f2e2cbea7e3a29e12344e6c6c18e928 Mon Sep 17 00:00:00 2001 From: Sangeeth Sudheer Date: Sun, 24 Apr 2022 16:50:53 +0530 Subject: [PATCH] longest-repeating-character-replacement py3 --- .../README.md | 26 +++++++++++ .../python3/sliding_window_1.py | 42 ++++++++++++++++++ .../python3/solution.py | 43 +++++++++++++++++++ 3 files changed, 111 insertions(+) create mode 100644 0424_longest-repeating-character-replacement/README.md create mode 100644 0424_longest-repeating-character-replacement/python3/sliding_window_1.py create mode 100644 0424_longest-repeating-character-replacement/python3/solution.py diff --git a/0424_longest-repeating-character-replacement/README.md b/0424_longest-repeating-character-replacement/README.md new file mode 100644 index 0000000..07e1c61 --- /dev/null +++ b/0424_longest-repeating-character-replacement/README.md @@ -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 \ No newline at end of file diff --git a/0424_longest-repeating-character-replacement/python3/sliding_window_1.py b/0424_longest-repeating-character-replacement/python3/sliding_window_1.py new file mode 100644 index 0000000..e2ed096 --- /dev/null +++ b/0424_longest-repeating-character-replacement/python3/sliding_window_1.py @@ -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 diff --git a/0424_longest-repeating-character-replacement/python3/solution.py b/0424_longest-repeating-character-replacement/python3/solution.py new file mode 100644 index 0000000..4f71b68 --- /dev/null +++ b/0424_longest-repeating-character-replacement/python3/solution.py @@ -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