# Answered! 4. Find Longest Repeated Pattern MUST BE RECURSIVE public int findLongest(char c, String s, int length) Given a String, find the longest sequence of matching characters in…

4. Find Longest Repeated Pattern

MUST BE RECURSIVE

public int findLongest(char c, String s, int length) Given a String, find the longest sequence of matching characters in that String and return that int length. For instance, “aaabbccccccdde” has 6 c’s so the method would return 6 Call this method passing it the first character in the String, the rest of the String and 1, as in findLongest(‘a’, s, 1) length represents the longest matching sequence found so far, which is 1 because we found ‘a’ Base case: if length==0 or length==1 return the length of s Recursive cases: if c matches the 0th character of s, then we extend the sequence, return the value of the recursive call with c, the rest of s after the 0th character, and length+1 else no match, return whichever is larger, length (the length we found of char c up to this point in s), or the value returned by recursively calling the method with the 0th character of s, the rest of s, and 1 as we are starting over Example: “aaabbccccccdde” Call the method with ‘a’, “aabbccccccdde”, 1 First character matches, so call with ‘a’, “abbccccccdde”, 2 First character matches, so call with ‘a’, “bbccccccdde, 3 No match, so return max(3, call with ‘b’, “bccccccdde”, 1) First character matches, so call with ‘b’, “ccccccdde, 2 No match, so return max(2, call with ‘c’, “cccccdde”, 1) First character matches, so call with ‘c’, “ccccdde”, 2 … Eventually these calls return 6 (6 c’s found) Return max(2, 6) Return max(3, 6) Run your program on the following inputs. “aabbbcddddddeefffghiii”, “19855aa00000cc24443bbb” , “33335557i322k555554p”, “2oo88rrrrr55ttyuuuuuuuuuuuuuuu44uuuuuuuuu4u444uuuuuuuuuuuuuuu” This is what I have so far…

public static int findLongest(char c, String s, int length) {

if (s.charAt(0) == s.charAt(1)) {

s = s.substring(1); length ++; System.out.println(s + length); findLongest(c, s, length);

}

else if (s.charAt(0) != s.charAt(1)) {

s = s.substring(1); length = 1; System.out.println(s + ” ” + length);

findLongest(c, s, length);

}

return length;

}

Thanks for any help!!

/program saved as FrequentCharacter.java

import java.util.Scanner;

public class FrequentCharacter {

public static void main(String[] args) {

//if you want to read the string from user use the below lines
/*Scanner input=new Scanner(System.in);
String srt1;
System.out.print(“nEnter a string: “);
str1=input.nextLine();*/

String str1=”apple banana orange”;
char ch=’ ‘;
int i=0;
boolean found=false;

//run a loop until a nonrepeating character is found
while(!found)
{
int charcount=0;
//get each char at a time
ch=str1.charAt(i);
//get character count by using countChar method
charcount=countChar(str1,ch );
//if charcount is 1 then set found=true
if(charcount==1)
{
found=true;
}
i++;//increment count
}

//display output
System.out.println(“First nonrepeating charater is ‘”+ch+”‘”);
}

//function that return the count of a character in a string
public static int countChar(String s,char c)
{
int count=0;
//loop through each character in the string and compare with c
for(int i=0;i<s.length();i++)
{
//if match found increment count
if(c==s.charAt(i))
{
count++;
}
}
//return count
return count;
}

}