5

在给定的字符串中,我想找出全部由相同字符组成的最大子字符串的起始索引,并计算该子字符串在主字符串中出现的次数。

例如:“aaakkkkkbbkkkkk” 在这种情况下,子字符串“kkkkk”的计数为 5,起始位置为 9。

到目前为止我的代码:

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);
}

我的问题:无法存储计数器值,因为该计数器值是子字符串的实际出现,稍后我将比较子字符串与计数器的出现。

我的逻辑也没有达到那个标准。

4

4 回答 4

3

我想这会对你有所帮助,我知道它很小而且很容易看到每一个,..

    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);



    }

祝你编码愉快..@All.....

于 2013-06-06T13:13:27.287 回答
2

1.制作单独的变量来存储子字符串的第一次出现

2.制作其他计数器,将存储最后一个序列长度

3.当新序列开始时重置计数器

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);
}
于 2013-06-06T12:14:08.833 回答
2

另一种解决方案:

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;
    }
}
于 2013-06-06T12:36:22.227 回答
1

此解决方案仅找到一个可能的子字符串,可能有几个子字符串具有相同的最大重复字符数。所以这是我的解决方案,它将返回一个列表。

这是代码:

导入 java.util.List;导入 java.util.ArrayList;

公共类子字符串 {

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);
    }

}

}

// 此示例字符串的输出是: 最长长度:2 是字符:1 开始于:3 最长长度:2 是字符:2 开始于:6 最长长度:2 是字符:3 开始于:9

于 2019-10-06T02:20:11.050 回答