Java Question Write Static Member Function Called Removelongestrepeatingsegment Takes Stri Q37256371

in java question:

Write a static member function calledremoveLongestRepeatingSegment that takes a String argument. Themethod will return a new String by removing the logest segment thatcontains consequtively repeating characters.

public static void main(String []args){

String s=”1111222223333311111111444455552222”;

String res= removeLongestRepeatingSegment(s); will return“11112222233333444455552222”

}

public static String removeLongestRepeatingSegment(Strings){


Answer


Your Code 1 public class Main { 2public static String removeLongestRepeatingSegment (String s)t int first e, last 0, max 1, c

public class Main {
public static String removeLongestRepeatingSegment(String s){
int first = 0, last = 0, max = 1, count = 1, temp = 0;
char curr = s.charAt(0);
for(int i=1; i<s.length(); i++)
{
if(s.charAt(i-1) == s.charAt(i))
count++;
else
{
if(max < count)
{
first = temp;
max

OR
OR

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.