-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExample2.java
More file actions
39 lines (32 loc) · 927 Bytes
/
Copy pathExample2.java
File metadata and controls
39 lines (32 loc) · 927 Bytes
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
package example.SlidingWindow;
public class Example2 {
public int longestKStringOfx(String s,int k,char x) {
int ans = 0;
int Nx = 0;
for(int right = 0;right<s.length()-k;right++) {
char c = s.charAt(right);
if (c==x)
Nx++;
int left = right-k;
if(left>=0) {
char cf = s.charAt(left);
if(cf==x)
Nx--;
}
if(Nx>ans)
ans=Nx;
}
return ans;
}
public static void main(String[] args) {
int k=3;
String s = "abaaaccsddaa";
char x = 'a';
Example1 example1 = new Example1();
System.out.println(example1.longestKStringOfx(s,k,x));
s = "abcdeaaadfewfsaaadassaaaaaddddaass";
k = 5;
x = 'a';
System.out.println(example1.longestKStringOfx(s,k,x));
}
}