Count and the starting index of the largest string that has all characters

In this line, I want to find out the initial index of the largest substring, all from the same character, and also calculate how many times this substring occurs in the main line.

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

My code is:

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 problems: it is impossible to save the counter value, because this counter value is the actual occurrence of the substring, and later I will compare the substring with the oncoming meeting.

I also do not adhere to this labeling with my logic.

+4
source share
4 answers

I think this will help you, I know it is small and very simple to see everyone, ..

  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 a happy coding .. @ All .....

+2
source

1. Create a separate variable to save the first occurrence of a substring

2. Start another counter that will save the last sequence length.

3. Reset the counter when a new sequence begins.

 String str = "aaakkkkbbkkkkk"; int count = 0; int firstOccurence = 0; int prevSequenceLength = 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 { //set the prevSequenceLength to counter value if it is less than counter value if(prevSequenceLength<counter) { prevSequenceLength=counter; //make firstOccurence to j - counter firstOccurence = j-counter; } //first reset the counter to 0; counter = 0; //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); } 
+2
source

Another solution:

 class Sample { static int max = 0, maxStartPos = -1; public static void main(String[] args) { search("aaakkkkbbkkkkk".toCharArray()); System.out.println(String.format("max len: %d, start pos: %d", max, maxStartPos)); search("xxxxxh".toCharArray()); System.out.println(String.format("max len: %d, start pos: %d", max, maxStartPos)); search("hxxxxxhyyyyyyh".toCharArray()); System.out.println(String.format("max len: %d, start pos: %d", max, maxStartPos)); } private static void search(char[] chars) { max = 0; maxStartPos = 0; int len = 0, startPos = 0; char start = chars[0]; for (int i = 0; i < chars.length; i++) { if (chars[i] == start) { len++; } else { if (len > max) { updateMaxResults(len, startPos); } startPos = i; len = 1; start = chars[i]; } } if (len > max) { updateMaxResults(len, startPos); } } private static void updateMaxResults(int len, int startPos) { max = len; maxStartPos = startPos; } } 
+2
source

This solution finds only one possible substring, there can be several substrings with the same maximum number of duplicate characters. So here is my solution that will return the list.

Here is the code:

import java.util.List; import java.util.ArrayList;

public class SubString {

 public static void main(String[] args) { String a1 = "12311m22n33"; char[] arrays = (a1+"\n").toCharArray(); //value is char +"_" + start List<String[]> lists = new ArrayList<>(); int len = 0; int start = -1; char prev = '\n'; int position= -1; int maxLen = 0; for (char a: arrays) { position++; if (a != prev) { if (len > maxLen ) { lists.clear(); String[] sq = {prev+"", start+""}; lists.add(sq); maxLen = len; } else if ( len == maxLen && len != 0) { String[] sq = {prev+"", start+""}; lists.add(sq); } prev = a; len = 1; start = position; } else { len++; } } for(String[] a: lists) { String s = a[0]; String st = a[1]; System.out.println("The longest length: " + maxLen + " is char: " + s + " start at : " + st); } } 

}

// output for this example line: Longest length: 2 characters: 1 starts with: 3 Longest length: 2 characters: 2, start at 6 Longest length: 2 characters: 3, start at 9

0
source

Source: https://habr.com/ru/post/1484819/


All Articles