Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count and the starting index of the largest string that has all chars same

In a given string I want to find out the starting index of the largest sub-string that's all made of the same character, and also count how many times that substring occurs in the main string.

ex: "aaakkkkbbkkkkk" In this case the count of the substring "kkkkk" is 5 and the starting position is 9.

My code so far:

String str = "aaakkkkbbkkkkk";
int count = 0;
//converting string into character array
char[] vals = str.toCharArray(); 

for(int i=0; i < vals.length; ){

   for(int j=i+1; j<vals.length; j++){
       //if value match then increment counter
       if(vals[i]== str.charAt(j)){
           counter++;
       }
       //else break from inner loop 
       break; //break from inner loop
   }
   //assign the index value of j to the i to start with new substring
   i = vals.indexOf(j);
}

My Issues: Unable to store counter value because this counter value is the actual occurrence of the substring, and later I'll compare substring with counter occurrence.

I also not upto that mark with my logic.

like image 847
Devendra Singh Avatar asked Jun 06 '13 12:06

Devendra Singh


People also ask

How do you find the index of all occurrences in a string?

Using indexOf() and lastIndexOf() method The String class provides an indexOf() method that returns the index of the first appearance of a character in a string. To get the indices of all occurrences of a character in a String, you can repeatedly call the indexOf() method within a loop.

What is the index value of first element of a string?

Strings are zero-indexed: The index of a string's first character is 0 , and the index of a string's last character is the length of the string minus 1.


1 Answers

I think this will help to you,, i know its small and very simple to see every one,..

    public static void main(String[] args) {


        String str = "aaakkkkkkkbbkkkkk";

        char[] var = str.toCharArray();

        for(int i=0; i<var.length;i++)
            System.out.print(" "+var[i]);


        int final_index=0; // this is for final index what we want

        int max_size=0; // for maximum no. of time the same char repeats continiously..
        int size=0; // this for finding size of sub string..
        char pre_char=var[0]; // pre_char is used check with present char in the array where the i position is shifted..



        // here is the loop..
        // we don't need much loops to this
        // while we are reading , the comparison is also done parallely.. 
        for(int i=1;i<var.length;i++) // here i starts from 1 because 0th char is shfted into pre_char 
            //and now comparion is starts from next char ie from 1th position
        {
//          System.out.println("i=="+i+" size "+size+"  prechar== "+pre_char+ "  var[i] ="+var[i]+" max size=="+max_size);
//          System.out.println("..........................");
            if(pre_char==var[i]) // here condition is checking. if its same with previous char, its means same char is occur again..
            {
                size+=1;
            }else{ // else means the sub string is has different char 
                if(max_size<size) // now check for whether any maximum size is occured priviously or not..
                {
                    max_size=size;
                    final_index=i-size;
                }
                size=0;
            }
            pre_char=var[i];
        }

        // now this for final 
        // its means if the max sub string is at the ending position, the above for loop breaks at the last element 
        // in this case we check if the last sub string is max or not..

        if(max_size<size)
        {
            max_size=size;
            final_index=var.length-size;
        }
        // then this is the final what we wana,,,,, 
        System.out.print("Max size is "+(max_size+1)+"  index is "+final_index);



    }

Have happy coding..@All.....

like image 187
Ramesh J Avatar answered Oct 03 '22 01:10

Ramesh J