Java 不使用字符串函数的字符串中子字符串的出现次数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19471930/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 17:34:09  来源:igfitidea点击:

Occurrences of a substring in a string without using string functions

javastring

提问by RTG87

I want to know how to count the occurrences of a particular sub-string in a string without using any of the built in JAVA string functions. For example:

我想知道如何在不使用任何内置 JAVA 字符串函数的情况下计算字符串中特定子字符串的出现次数。例如:

InputString = "knowbutuknow"
subString = "know"

The program should return the result as 2.

程序应该返回结果为 2。

EDIT: Re-phrased my question. This is one of those interview questions I came across.

编辑:重新表述我的问题。这是我遇到的面试问题之一。

EDIT: Basic string functions like charAt and length can be used here.

编辑:可以在此处使用基本字符串函数,如 charAt 和 length。

采纳答案by dognose

Assuming, you already know the keyword you are searching for:

假设您已经知道要搜索的关键字:

  • Start at char "0" of Input String
  • Iterate until "length - keyWordLength" (keyword of length 4 can not match into the last 3 chars)
  • Inside: Iterate from 0 to keyWord.length -1 and always compare:
  • Char at Position of outer loop PLUS position of inner loop of the Input string with the char at "inner loops" position of the keyword.
  • if you find a match, go ahead with the innerloop, if it does not match, advance the outer loop, by simple "breaking" the inner loop.
  • If you have a match, and completely processed the inner loop, you have a match of that keyword.
  • 从输入字符串的字符“0”开始
  • 迭代直到“length - keyWordLength”(长度为4的关键字不能匹配到最后3个字符)
  • 内部:从 0 迭代到 keyWord.length -1 并始终比较:
  • 外循环位置的字符加上输入字符串的内循环位置,字符位于关键字的“内循环”位置。
  • 如果找到匹配项,则继续进行内循环,如果不匹配,则通过简单的“破坏”内循环来推进外循环。
  • 如果您有匹配项,并且完全处理了内部循环,那么您就有了该关键字的匹配项。

Something like this. I'm Assuming String.lengthto be allowed. Otherwhise you would need to create your own strlen function. (This can be achieved, using a forachloop and simple counting "up")

像这样的东西。我假设String.length被允许。否则,您需要创建自己的 strlen 函数。(这可以实现,使用forach循环和简单的“向上”计数)

This is untested and may not work out of the box, but should give you a brief idea.

这是未经测试的,可能无法开箱即用,但应该给您一个简短的想法。

String inputString = "knowbutuknow";
String subString = "know";

int matches = 0;
for (int outer = 0; outer <= inputString.length() - subString.length(); outer++){
  for (int inner = 0; inner < subString.length(); inner++){
    if (inputString.charAt(outer + inner) == subString.charAt(inner)){
      // letter matched, proceed.
      if (inner == subString.length()-1){
        //last letter matched, so a word match at position "outer"
        matches++;
        //proceed with outer. Room for improvement: Skip next n chars beeing
        // part of the match already.
        break;
      } 
    }else{
      //no match for "outer" position, proceed to next char.
      break;
    }
  }
} 

edit: Sorry, mixed in some php :) fixed it.

编辑:抱歉,混入了一些 php :) 修复了它。