Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

package eecs.hm; public class M2 { /* Given a non-empty string,determine the lon

ID: 3808022 • Letter: P

Question

 package eecs.hm;  public class M2 {     /*     Given a non-empty string,determine the longest sequence of repeating characters     in it (i.e. consecutive characters that are the same) and return the repeating     character and its repetition count delimited by an equal sign. If more than one     such sequence is found, the return should be about the one that occurs first in     the string.      Example: longest("ZZZZAISSDRRIIIIIIIHHHQQQDDDDDDDZZZZZDD") returns "I=7" because     there are two longest repeating sequences in this string (D and I, each repeating     7 times) and the I sequence occurs before the D sequence.      */     public static String longest(String s)     {         return null;  // delete me!     } } 

Explanation / Answer

Source code:-

import java.io.*;

public class M2{
public static void main(String[] args) {
String setofletters = "aaakkcccccczz"; /* 15 */
int output = Longest(setofletters);
System.out.println("Longest run that first appeared in index:" + output);
}
   public static int Longest(String setofletters){
int ctr = 1;
int output = 0;
int j=0;
for(int i=0; i<setofletters.length()-1;i++){
j=i;
while(i <setofletters.length()-1 && setofletters.charAt(i)==setofletters.charAt(i+1)){
i++;
ctr++;
}
if(ctr>output){
output=j;
}
ctr = 1;
}
return output;
}
}