-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLongest_Repeating_Character_Replacement.java
More file actions
48 lines (48 loc) · 1.43 KB
/
Longest_Repeating_Character_Replacement.java
File metadata and controls
48 lines (48 loc) · 1.43 KB
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
import java.util.*;
public class Longest_Repeating_Character_Replacement {
class Solution {
public int characterReplacement(String s, int k) {
int n = s.length();
int[] hashing = new int[26];
int l = 0;
int r = 0;
int maxlen = 0;
int maxfreq = 0;
int sum_total = 0;
while(r<n)
{
hashing[s.charAt(r) - 'A']++;
sum_total = 0;
for(int i = 0 ; i<26 ; i++)
{
if(maxfreq<hashing[i])
{
maxfreq = hashing[i];
}
sum_total += hashing[i];
}
while(sum_total - maxfreq > k)
{
hashing[s.charAt(l) - 'A']--;
l++;
sum_total = 0;
maxfreq = 0;
for(int i = 0 ; i<26 ; i++)
{
if(maxfreq<hashing[i])
{
maxfreq = hashing[i];
}
sum_total += hashing[i];
}
}
if(sum_total - maxfreq <= k)
{
maxlen = Math.max(maxlen , r-l+1);
}
r++;
}
return maxlen;
}
}
}