-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path424.替换后的最长重复字符.java
62 lines (54 loc) · 1.67 KB
/
424.替换后的最长重复字符.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
* @lc app=leetcode.cn id=424 lang=java
*
* [424] 替换后的最长重复字符
*/
// @lc code=start
class Solution {
public int characterReplacement(String s, int k) {
// mine
// int result = 0;
// Set<Character> chars = new HashSet<Character>();
// for (char c : s.toCharArray()) {
// if (!chars.contains(c)) {
// result = Math.max(result, longestOnes(s, c, k));
// }
// chars.add(c);
// }
// return result;
// Solution from Nick White
int n = s.length();
int[] charCounts = new int[26];
int windowStart = 0;
int maxLength = 0;
int maxCount = 0;
for (int windowEnd = 0 ; windowEnd < n ; windowEnd++) {
charCounts[s.charAt(windowEnd) - 'A']++;
int currentCharCount = charCounts[s.charAt(windowEnd) - 'A'];
maxCount = Math.max(maxCount, currentCharCount);
while (windowEnd - windowStart + 1 > maxCount + k) {
charCounts[s.charAt(windowStart) - 'A']--;
windowStart++;
}
maxLength = Math.max(maxLength, windowEnd - windowStart + 1);
}
return maxLength;
}
// private int longestOnes(String s, char c, int k) {
// int i = 0;
// int j = 0;
// int length = s.length();
// while (i < length) {
// if (s.charAt(i) != c) k--;
// if (k < 0) {
// if (s.charAt(j) != c) {
// k++;
// }
// j++;
// }
// i++;
// }
// return i - j;
// }
}
// @lc code=end