AdSense

Saturday, October 29, 2016

Longest Repeating Character Replacement

Given a string that consists of only uppercase English letters, you can replace any letter in the string with another letter at 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 will not exceed 104.
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.

Update 2016-11-12

A more explainable solution:


public int characterReplacement(String s, int k) {
        int len = s.length();
        if (len == 0) {
            return 0;
        }
        
        int[] count = new int[26];
        int max = 0;
        int start = 0;
        for (int end = 0; end < len; end++) {
            max = Math.max(max, ++count[s.charAt(end) - 'A']);
            if (max + k < end - start + 1) {
                count[s.charAt(start++) - 'A']--;
            }
            
        }
        return len - start;
    }


The idea is that the window never needs to shrink. After the length of the substring expands to maximum, when we add one more char, if it is the char with the maximum occurrence, nothing changes, otherwise, the length of the substring increases but max + k doesn't, so we need to move window leftward (start++). In the following rounds, until we find another char has more occurrence, we only need to move window to keep the max length correct. So "max" here is defined as the maximum occurrence seen so far, not the maximum occurrence in current window.





This problem asks us to find the longest substring of repeating characters if we can make at most k replacement. Now if we remove the restriction that we can only make k replacement, the solution should be length of string - occurrence of character with maximum occurrence. With the k restriction, we can use sliding window manner, if length of the window - max occurrence so far > k, we know we cannot construct such substring with at most k replacement, so we move window rightward. The longest substring should be updated with the length of the window.


public int characterReplacement(String s, int k) {
        if (s.length() == 0) {
            return 0;
        }
        int start = 0;
        int len = s.length();
        int[] count = new int[26];
        int maxCount = 0;
        int rst = 0;
        for (int i = 0; i < len; i++) {
            maxCount = Math.max(maxCount, ++count[s.charAt(i) - 'A']);
            while (i - start + 1 - maxCount > k) {
                maxCount = Math.max(maxCount, --count[s.charAt(start) - 'A']);
                start++;
            }
            rst = Math.max(rst, i - start + 1);
        }
        return rst;
    }


3 comments:

  1. i think that max_count in line 13 is not updated at any cost because every time you are decreasing the count, maxCount is always greater.

    ReplyDelete

  2. The development of artificial intelligence (AI) has propelled more programming architects, information scientists, and different experts to investigate the plausibility of a vocation in machine learning. Notwithstanding, a few newcomers will in general spotlight a lot on hypothesis and insufficient on commonsense application. IEEE final year projects on machine learning In case you will succeed, you have to begin building machine learning projects in the near future.

    Projects assist you with improving your applied ML skills rapidly while allowing you to investigate an intriguing point. Furthermore, you can include projects into your portfolio, making it simpler to get a vocation, discover cool profession openings, and Final Year Project Centers in Chennai even arrange a more significant compensation.


    Data analytics is the study of dissecting crude data so as to make decisions about that data. Data analytics advances and procedures are generally utilized in business ventures to empower associations to settle on progressively Python Training in Chennai educated business choices. In the present worldwide commercial center, it isn't sufficient to assemble data and do the math; you should realize how to apply that data to genuine situations such that will affect conduct. In the program you will initially gain proficiency with the specialized skills, including R and Python dialects most usually utilized in data analytics programming and usage; Python Training in Chennai at that point center around the commonsense application, in view of genuine business issues in a scope of industry segments, for example, wellbeing, promoting and account.

    ReplyDelete