-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLongestRepeatingCharReplace.java
More file actions
53 lines (36 loc) · 1.27 KB
/
LongestRepeatingCharReplace.java
File metadata and controls
53 lines (36 loc) · 1.27 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
49
50
51
52
53
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
public class LongestRepeatingCharReplace {
//Finds the character with max Frequency
public static int maxFreq(HashMap<Character, Integer> map){
int max = Collections.max(map.values());
for(Map.Entry<Character, Integer> e: map.entrySet()){
if(e.getValue() == max){
return e.getValue();
}
}
return 0;
}
public static int characterReplacement(String s, int k) {
HashMap<Character, Integer> map = new HashMap<>();
int l=0, res=0;
for(int r=0; r < s.length();r++){
if(map.containsKey(s.charAt(r)))
map.put(s.charAt(r), map.get(s.charAt(r))+1);
else
map.put(s.charAt(r), 1);
while((r-l+1) - maxFreq(map) > k){
map.put(s.charAt(l), map.get(s.charAt(l))-1);
l=l+1;
}
res= Math.max(r-l+1, res);
}
return res;
}
public static void main(String args[]){
String s = "ABAB";
int a = characterReplacement(s, 1);
System.out.println(a);
}
}