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