Skip to content

Commit cbf2465

Browse files
authored
Merge pull request akgmage#1783 from akgmage/dev
add longest_repeated_character_replacement in python
2 parents 5368f3f + 11ab5c8 commit cbf2465

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
'''
2+
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.
3+
4+
Return the length of the longest substring containing the same letter you can get after performing the above operations.
5+
6+
Example 1:
7+
8+
Input: s = "ABAB", k = 2
9+
Output: 4
10+
Explanation: Replace the two 'A's with two 'B's or vice versa.
11+
12+
Summary:
13+
The provided code defines a Java class `Solution` with a method `characterReplacement` that aims to find the
14+
longest substring within the input string `s` such that it can be created by replacing at most `k` characters
15+
with any other character. It uses a sliding window approach to efficiently compute the maximum length of such a
16+
substring.
17+
18+
Time Complexity:
19+
- The code iterates through the input string `s` using a sliding window with two pointers
20+
(startWindow and endWindow). During each iteration, it updates character counts and evaluates the
21+
maximum length of a valid substring. Since each character is processed exactly once, the time complexity
22+
is O(N), where N is the length of the input string `s`.
23+
24+
Space Complexity:
25+
- The code uses additional space to store integer variables (`count`, `startWindow`, `maxCount`, and `max`).
26+
The `count` array has a fixed size of 26 (for 26 English alphabet letters). Therefore, the space complexity is
27+
O(1), as the space used is constant and does not depend on the input size.
28+
29+
In summary, the algorithm has a time complexity of O(N) and a space complexity of O(1), making it efficient
30+
for finding the longest substring with at most 'k' replacements in a given string.
31+
'''
32+
class Solution:
33+
def characterReplacement(self, s: str, k: int) -> int:
34+
count = [0] * 26 # Initialize a list to count the occurrences of characters (26 letters in the English alphabet)
35+
startWindow = 0 # The left end of the sliding window
36+
maxCount = 0 # The maximum count of any character within the window
37+
max_length = 0 # The maximum length of a substring that can be formed
38+
39+
# Iterate through the string using a sliding window approach
40+
for endWindow in range(len(s)):
41+
val = ord(s[endWindow]) - ord('A') # Convert the character to an index (0-25)
42+
count[val] += 1 # Increment the count for the current character
43+
maxCount = max(maxCount, count[val]) # Update the maximum character count
44+
45+
# While the length of the current window minus the maximum character count exceeds 'k', shrink the window
46+
while endWindow - startWindow + 1 - maxCount > k:
47+
val = ord(s[startWindow]) - ord('A') # Get the character at the start of the window
48+
count[val] -= 1 # Decrement the count for the character at the start of the window
49+
startWindow += 1 # Move the start of the window to the right
50+
51+
# Update the maximum length of a substring that can be formed
52+
max_length = max(max_length, endWindow - startWindow + 1)
53+
54+
# Return the maximum length, which represents the longest substring with at most 'k' replacements
55+
return max_length

0 commit comments

Comments
 (0)