Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find the longest sequence of same characters in a string

Tags:

java

This code supposed to output the longest run on which a character in a string has a consecutive runs of itself. Though the problem is that it outputs: 8 (which should be 5 instead). I just would like to ask what seems to be the problem regarding this code.

public class Sample {
    public static void main(String[] args) {
        String setofletters = "aaakkcccccczz"; /* 15 */
        int output = runLongestIndex(setofletters);
        System.out.println("Longest run that first appeared in index:" + output);
    }

    public static int runLongestIndex(String setofletters) {
        int ctr = 0;
        int ctrstor = 0;
        int ii = 0;
        int output = 0;

        // loops until the last character in the string
        for (int i = 0; i < setofletters.length() - 1; i++) {
            // checks if the letter is the same to the next
            if (setofletters.charAt(i) == setofletters.charAt(i++)) {
                ctr++;
                ii = i++;
                // loops until the letter in the index is no longer equal
                while (setofletters.charAt(i) == setofletters.charAt(ii)) {
                    ii++;
                    ctr++;
                }

                if (ctr > ctrstor) {
                    output = i;
                }
                // storing purposes
                ctrstor = ctr;
            }
            // resets the counter
            ctr = 0;
        }
        return output;
    }
}
like image 699
Snail Avatar asked Aug 05 '15 18:08

Snail


1 Answers

UPDATE Sorry, I misunderstood your question a bit, you need to make the following changes in your code to make it work.(lines with comments)

   public static int runLongestIndex(String setofletters){
    int ctr = 1; // every character is repeated at least once, so you should initialize it to 1, not 0
    int ctrstor = 0;
    int ii = 0;
    int output = 0;

    for (int i = 0; i < setofletters.length() - 1; i++) {
        if (i < setofletters.length() - 1 && setofletters.charAt(i) == setofletters.charAt(i+1)) { // i++ is not same as i+1
            ctr++;
            ii = i+1; // i++ is not same as i+1
            while (setofletters.charAt(i) == setofletters.charAt(ii)) {
                ii++;
                ctr++;
            }

            if (ctr > ctrstor) {
                output = i;
            }
            ctrstor = ctr;
        }
        ctr = 1; // for the same reason I mentioned above
    }
    return output;
}

EDIT : the easiest way to write your code is :

public static int runLongestIndex(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;
}

Why are you assigning i to output? You should assign ctr to output.

change

   if(ctr>ctrstor){
      output=i;
   }

to

 if(ctr>ctrstor){
      output=ctr;
 }

and also I think you should change

 if(setofletters.charAt(i)==setofletters.charAt(i++))

to

 if(i<setofletters.length()-1 && setofletters.charAt(i)==setofletters.charAt(i+1)){

and you should intialize ctr to 1 but not 0 because every character is repeated at least once.

like image 181
Karthik Avatar answered Oct 10 '22 16:10

Karthik