-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApple Sequences.java
More file actions
67 lines (57 loc) · 1.52 KB
/
Apple Sequences.java
File metadata and controls
67 lines (57 loc) · 1.52 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
//{ Driver Code Starts
//Initial Template for Java
import java.io.*;
import java.util.*;
class GFG
{
public static void main(String args[])throws IOException
{
BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out=new PrintWriter(System.out);
int t = Integer.parseInt(read.readLine());
while(t-- > 0)
{
String input[] = read.readLine().trim().split("\\s+");
int N = Integer.parseInt(input[0]);
int M = Integer.parseInt(input[1]);
String arr = read.readLine().trim();
Solution ob = new Solution();
out.println(ob.appleSequence(N, M, arr));
}
out.close();
}
}
// } Driver Code Ends
//User function Template for Java
class Solution{
public static int appleSequence(int n, int m, String arr){
//code here
int start=0;
int cnt=0;
int max=0;
char c[]=arr.toCharArray();
for(int i=0;i<n;i++)
{
if(c[i]=='A')
{
max=Math.max(max,i-start+1);
}
if(c[i]=='O')
{
cnt++;
if(cnt>m)
{
while(start<i && c[start]=='A')
{
start++;
}
start++;
}
max=Math.max(max,i-start+1);
}
}
return max;
}
}
//{ Driver Code Starts.
// } Driver Code Ends