Occurrences of substring in a string

后端 未结 24 2180
眼角桃花
眼角桃花 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: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;
    

    }

提交回复
热议问题