Occurrences of substring in a string

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

    This below method show how many time substring repeat on ur whole string. Hope use full to you:-

        String searchPattern="aaa"; // search string
        String str="aaaaaababaaaaaa"; // whole string
        int searchLength = searchPattern.length(); 
        int totalLength = str.length(); 
        int k = 0;
        for (int i = 0; i < totalLength - searchLength + 1; i++) {
            String subStr = str.substring(i, searchLength + i);
            if (subStr.equals(searchPattern)) {
               k++;
            }
    
        }
    

提交回复
热议问题