[Leetcode] Longest repeating Character replacement maximum repeating character substitution

Source: Internet
Author: User

Given A string that consists of only uppercase 中文版 letters, you can replace any letter in the string with another lett Er at the most K times. Find the length of a longest substring containing all repeating letters you can get after performing the above operations.

Note:
Both the string ' s length and K would not exceed 104.

Example 1:

Input:s = "ABAB", K = 2Output:4Explanation:Replace the ' A ' A ' s with ' B ' or vice versa.

Example 2:

Input:s = "Aababba", K = 1Output:4Explanation:Replace the One ' A ' in the middle with ' B ' and fo RM "Aabbbba". The substring "BBBB" has the longest repeating letters, and which is 4.

This problem gives us a string that says we have the chance to randomly displace any character at random, let's find the longest repeating character of the string. This problem is similar to the previous longest Substring with at the most K Distinct characters, all need to use the sliding window to sliding window to solve. Let's start by thinking that if there is no k limit, let's change the string to the minimum number of permutations required for a string with only one character repetition, that is, the total length of the string minus the most occurrences of the number of characters. If we add k to the limit, we are actually asking for satisfaction (the length of the substring minus the number of characters that occur most) the maximum substring length of the <=k, and we should know how to use the sliding window to solve it. We use a variable start to record the left edge of the sliding window, Initialized to 0, and then we iterate through the string, each time we accumulate the number of characters, and then update the number of the most characters, and then we determine whether the current sliding window satisfies the previously said condition, if not satisfied, we will move the left edge of the sliding window to the right one, and note that the removed characters in the counts minus one , until the condition is met, we can update the result res, see the code below:

classSolution { Public:    intCharacterreplacement (stringSintk) {intres =0, maxcnt =0, start =0; Vector<int> Counts ( -,0);  for(inti =0; I < s.size (); ++i) {maxcnt= Max (maxcnt, ++counts[s[i]-'A']);  while(I-start +1-MAXCNT >k) {--counts[s[start]-'A']; ++start; } Res= Max (res, I-start +1); }        returnRes; }};

Similar topics:

Longest Substring with at Least K repeating characters

Longest Substring with at the most K Distinct characters

Longest Substring with at the most of the Distinct characters

Longest Substring without repeating characters

Resources:

Https://discuss.leetcode.com/topic/63494/java-12-lines-o-n-sliding-window-solution-with-explanation/2

Https://discuss.leetcode.com/topic/63416/sliding-window-similar-to-finding-longest-substring-with-k-distinct-characters/2

Leetcode all in one topic summary (continuous update ...)

[Leetcode] Longest repeating Character replacement maximum repeating character substitution

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.