Skip to content

Commit 8dba7e2

Browse files
committed
add longest_repeated_character_replacement in go
1 parent c80079d commit 8dba7e2

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+
func characterReplacement(s string, k int) int {
33+
count := make([]int, 26) // Initialize an array to count the occurrences of characters (26 letters in the English alphabet)
34+
startWindow := 0 // The left end of the sliding window
35+
maxCount := 0 // The maximum count of any character within the window
36+
max := 0 // The maximum length of a substring that can be formed
37+
38+
// Iterate through the string using a sliding window approach
39+
for endWindow := 0; endWindow < len(s); endWindow++ {
40+
val := int(s[endWindow] - 'A') // Convert the character to an index (0-25)
41+
count[val]++ // Increment the count for the current character
42+
maxCount = maxInt(maxCount, count[val]) // Update the maximum character count
43+
44+
// While the length of the current window minus the maximum character count exceeds 'k', shrink the window
45+
for endWindow - startWindow + 1 - maxCount > k {
46+
val = int(s[startWindow] - 'A') // Get the character at the start of the window
47+
count[val]-- // Decrement the count for the character at the start of the window
48+
startWindow++ // Move the start of the window to the right
49+
}
50+
51+
// Update the maximum length of a substring that can be formed
52+
max = maxInt(max, endWindow - startWindow + 1)
53+
}
54+
55+
// Return the maximum length, which represents the longest substring with at most 'k' replacements
56+
return max
57+
}
58+
59+
func maxInt(a, b int) int {
60+
if a > b {
61+
return a
62+
}
63+
return b
64+
}

0 commit comments

Comments
 (0)