Occurrences of substring in a string

后端 未结 24 1756
眼角桃花
眼角桃花 2020-11-22 03:35

Why is the following algorithm not halting for me? (str is the string I am searching in, findStr is the string I am trying to find)

String str = \"helloslkhe         


        
相关标签:
24条回答
  • 2020-11-22 03:40

    This solution prints the total number of occurrence of a given substring throughout the string, also includes the cases where overlapping matches do exist.

    class SubstringMatch{
        public static void main(String []args){
            //String str = "aaaaabaabdcaa";
            //String sub = "aa";
            //String str = "caaab";
            //String sub = "aa";
            String str="abababababaabb";
            String sub = "bab";
    
            int n = str.length();
            int m = sub.length();
    
            // index=-1 in case of no match, otherwise >=0(first match position)
            int index=str.indexOf(sub), i=index+1, count=(index>=0)?1:0;
            System.out.println(i+" "+index+" "+count);
    
            // i will traverse up to only (m-n) position
            while(index!=-1 && i<=(n-m)){   
                index=str.substring(i, n).indexOf(sub);
                count=(index>=0)?count+1:count;
                i=i+index+1;  
                System.out.println(i+" "+index);
            }
            System.out.println("count: "+count);
        }
    }
    
    0 讨论(0)
  • 2020-11-22 03:41

    Your lastIndex += findStr.length(); was placed outside the brackets, causing an infinite loop (when no occurence was found, lastIndex was always to findStr.length()).

    Here is the fixed version :

    String str = "helloslkhellodjladfjhello";
    String findStr = "hello";
    int lastIndex = 0;
    int count = 0;
    
    while (lastIndex != -1) {
    
        lastIndex = str.indexOf(findStr, lastIndex);
    
        if (lastIndex != -1) {
            count++;
            lastIndex += findStr.length();
        }
    }
    System.out.println(count);
    
    0 讨论(0)
  • 2020-11-22 03:42

    A shorter version. ;)

    String str = "helloslkhellodjladfjhello";
    String findStr = "hello";
    System.out.println(str.split(findStr, -1).length-1);
    
    0 讨论(0)
  • 2020-11-22 03:42

    Increment lastIndex whenever you look for next occurrence.

    Otherwise it's always finding the first substring (at position 0).

    0 讨论(0)
  • 2020-11-22 03:43

    Try this one. It replaces all the matches with a -.

    String str = "helloslkhellodjladfjhello";
    String findStr = "hello";
    int numberOfMatches = 0;
    while (str.contains(findStr)){
        str = str.replaceFirst(findStr, "-");
        numberOfMatches++;
    }
    

    And if you don't want to destroy your str you can create a new string with the same content:

    String str = "helloslkhellodjladfjhello";
    String strDestroy = str;
    String findStr = "hello";
    int numberOfMatches = 0;
    while (strDestroy.contains(findStr)){
        strDestroy = strDestroy.replaceFirst(findStr, "-");
        numberOfMatches++;
    }
    

    After executing this block these will be your values:

    str = "helloslkhellodjladfjhello"
    strDestroy = "-slk-djladfj-"
    findStr = "hello"
    numberOfMatches = 3
    
    0 讨论(0)
  • 2020-11-22 03:43
    public static int getCountSubString(String str , String sub){
    int n = 0, m = 0, counter = 0, counterSub = 0;
    while(n < str.length()){
      counter = 0;
      m = 0;
      while(m < sub.length() && str.charAt(n) == sub.charAt(m)){
        counter++;
        m++; n++;
      }
      if (counter == sub.length()){
        counterSub++;
        continue;
      }
      else if(counter > 0){
        continue;
      }
      n++;
    }
    
    return  counterSub;
    

    }

    0 讨论(0)
提交回复
热议问题