-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLongestRepeatingCharacterReplacement.java
34 lines (32 loc) · 1.16 KB
/
LongestRepeatingCharacterReplacement.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
public class LongestRepeatingCharacterReplacement {
// Driver code.
public static void main(String[] args){
String st = "AAAA";
int n = 1;
LongestRepeatingCharacterReplacement o1 = new LongestRepeatingCharacterReplacement();
System.out.print(o1.longestRepeatingCharacterAfterReplacement(st, n));
}
public int longestRepeatingCharacterAfterReplacement(String st, int n){
String temp = "";
int dis = 0;
int maxLength = -1;
for (char c : st.toCharArray()) {
String current = String.valueOf(c);
if(!temp.contains(current)) {
dis++;
}
temp = temp + String.valueOf(current); // Warning:(19, 27) Unnecessary 'String.valueOf()' call; Warning:(19, 25) String concatenation '+' in loop
if (temp.length() - dis > n) {
temp = temp.substring(1);
dis--;
}
maxLength = Math.max(maxLength, temp.length());
}
return maxLength;
}
// 方法二
/*Created by @Kurt LEE On 1/15/23 3:39 AM.*/
public int findLength(String s, int k) {
return 0;
}
}