Formally, given the results of n tosses of a coin, find the maximum number of consecutive Heads and the maximum number of consecutive Tails.
Consider the following example: a coin was tossed n=7 times and the results were Heads
Tails, Tails, Tails, Heads, Heads, Tails. Therefore, the longest Heads streak was 3 and the longest Tails streak was 2.
Complete the function getMaxStreaks which takes an array of strings toss and returns an array of two integers denoting the maximum streaks of Heads and Tails respectively.
Input Format
In the first line, there is a single integer n denoting the number of tosses.
Then, n lines follow. The ith of them contains a string tossi denoting the result of the ith toss of the coin.
Constraints
tossi € { Heads, Tails}
Output Format
In a single line, print two space-separated integers denoting the maximum streak of Heads and the maximum streak of Tails respectively.
Sample Input 07
Heads
Tails
Tails
Tails
Heads
Heads
Tails
Sample Output 0
2 3
Explanation 0
The longest streak of Heads is 2 and the longest streak of Tails is 3.
Sample Input 13
Tails
Tails
Tails
Sample Output 1
0 3
Explanation 1
The longest streak of Heads is 0 since there were no such tosses, and the longest streak of Tails is 3.
Sample Input 24
Heads
Heads
Heads
Heads
Sample Output 2
4 0
Explanation 2
The longest streak of Heads is 4, and the longest streak of Tails is 0 since there were no such tosses.
Solutionimport java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
List<String> l = new ArrayList<>();
int n=sc.nextInt();
for(int i=0;i<n;i++)
{
String str=sc.next();
l.add(str);
}
getMaxStreaks(l);
}
public static List<Integer> getMaxStreaks(List<String> toss) {
List<Integer> listR = new ArrayList<>();
int headCountM = 0;
int tailCountM = 0;
int headCount = 0;
int tailCount = 0;
for (String t : toss) {
if (t.equals("Heads")) {
tailCount = 0;
headCount++;
if (headCountM < headCount) {
headCountM = headCount;
}
} else {
headCount = 0;
tailCount++;
if (tailCountM < tailCount) {
tailCountM = tailCount;
}
}
}
listR.add(headCountM);
listR.add(tailCountM);
System.out.print(headCountM+" ");
System.out.print(tailCountM+" ");
return listR;
}
}