CS기초/코딩테스트

LeetCode #424

오늘의 나1 2022. 12. 21. 20:25

https://leetcode.com/problems/longest-repeating-character-replacement/description/

 

Longest Repeating Character Replacement - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

 

제일 속도가 빠른 해답은 이건 데,

count[s.charAt(start) - 'A']--해도 maxCount가 계산되는 것이 아직 이해가 안간다.

class Solution {
    public int characterReplacement(String s, int k) {
        int len = s.length();
        int maxLength =0, start=0, maxCount=0;
        int[] count = new int[26];

        for(int end =0; end<len; end++) {
          maxCount = Math.max(maxCount,++count[s.charAt(end) - 'A'] );
          while(end - start + 1 - maxCount > k){ //end - start + 1 is size of window
          	  // maxCount + k가 경우의 수로 나올 수 있는 가장 작은 maxLen이기 때문에
              // maxCount + k 이상인 경우나, 이하일 때는 넘긴다?
              count[s.charAt(start) - 'A']--;
              start++;
          }
            maxLength = Math.max( maxLength, end-start + 1);
        }
    return maxLength;        
  }
}