-
-
Notifications
You must be signed in to change notification settings - Fork 300
/
424.java
72 lines (67 loc) · 2.38 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
62
63
64
65
66
67
68
69
70
71
72
__________________________________________________________________________________________________
sample 2 ms submission
class Solution {
public int characterReplacement(String s, int k) {
if (s.length() <= k){
return s.length();
}
int[] hash = new int[256];
int l = 0, r = 0, max = 0, res = 0;
char[] chs = s.toCharArray();
while (r < chs.length){
hash[chs[r]]++;
if (hash[chs[r]] > max){
max = hash[chs[r]];
}
r++;
while(r - l - max > k){
hash[chs[l++]]--;
}
res = Math.max(res, r - l);
}
return res;
}
}
__________________________________________________________________________________________________
sample 35112 kb submission
class Solution {
public static class CharacterCount {
int count=0;
void incr() { count++; }
void decr() { count--; }
}
public static void addCharacter(PriorityQueue<CharacterCount> pq, CharacterCount count) {
pq.remove(count);
count.incr();
pq.add(count);
}
public static void removeCharacter(PriorityQueue<CharacterCount> pq, CharacterCount count) {
pq.remove(count);
count.decr();
pq.add(count);
}
public static int characterReplacement(String s, int k) {
if (s.isEmpty())
return 0;
if (s.length() == 1)
return 1;
CharacterCount[] characterCounts = new CharacterCount[26];
for(int i=0; i<characterCounts.length; i++) {
characterCounts[i] = new CharacterCount();
}
PriorityQueue<CharacterCount> pq = new PriorityQueue<>(characterCounts.length, (c1, c2) -> Integer.compare(c2.count, c1.count));
int begin=0;
int maxLength = Integer.MIN_VALUE;
characterCounts[s.charAt(0)-'A'].incr();
pq.add(characterCounts[s.charAt(0)-'A']);
for(int i=1; i<s.length(); i++) {
addCharacter(pq, characterCounts[s.charAt(i)-'A']);
while(pq.peek().count+k < i-begin+1) {
removeCharacter(pq, characterCounts[s.charAt(begin++)-'A']);
}
maxLength = Integer.max(maxLength, i-begin+1);
}
return maxLength;
}
}
__________________________________________________________________________________________________