Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

4. Find Longest Repeated Pattern public int findLongest(char c, String s, int length) Given a String, find the longest sequence of matching characters in that

4. Find Longest Repeated Pattern 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 cs 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 cs 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!!

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Essentials of Database Management

Authors: Jeffrey A. Hoffer, Heikki Topi, Ramesh Venkataraman

1st edition

133405680, 9780133547702 , 978-0133405682

More Books

Students also viewed these Databases questions

Question

How many three-digit numbers are divisible by 7?

Answered: 1 week ago

Question

What is Indian Polity and Governance ?

Answered: 1 week ago