[英]How do I check if the preceding letter of the word that I am looking for in a string exist? Recursion
I am currently creating a java program that counts the number of occurrences of a specific word in a string using recursion, however, if the preceding letter is an 'a', the count won't be incremented.我目前正在创建一个 java 程序,该程序使用递归计算字符串中特定单词的出现次数,但是,如果前面的字母是“a”,则计数不会增加。 I cannot find a way to check the preceding letter before the first letter of the word I am looking for.
我找不到一种方法来检查我要查找的单词的第一个字母之前的前一个字母。 I tried using indexOf then subtracting one to check the preceding letter, but it won't work.
我尝试使用 indexOf 然后减去一个来检查前面的字母,但它不起作用。
Here's my working function at the moment:这是我目前正在工作的 function :
//The value of text is abrichbbarichacrich
//While the value of find is rich
//Expected output should be 2
static int Count(String text, String find) {
if (text.length() == 0 || text.length() < find.length()) {
return 0;
}
if (text.contains(find)) {
return 1 + Count(text.replaceFirst(find, ""), find);
}
return 0;
}
Here's my second version, but it gives me a StringIndexOutOfBoundsException and the output should be 2, but instead it gives me an output of 3.这是我的第二个版本,但它给了我一个 StringIndexOutOfBoundsException 并且 output 应该是 2,但它给了我一个 3 的 output。
if (text.length() == 0 || text.length() < find.length()) {
return 0;
}
if (text.contains(find)) {
int index = text.indexOf(find) - 1;
if (text.charAt(index) == 'a')
return Count(text.replaceFirst(find, ""), find);
return 1 + Count(text.replaceFirst(find, ""), find);
}
Any help would be appreciated:)任何帮助,将不胜感激:)
Correct if else condition.replaceFirst() method returns the updated string.We have to update the string.更正 if else condition.replaceFirst() 方法返回更新后的字符串。我们必须更新字符串。
static int Count(String text, String find) {
if (text.length() == 0 || text.length() < find.length()) {
return 0;
}
if (text.contains(find)) {
int index = text.indexOf(find) - 1;
text=text.replaceFirst(find, "");
if (index!=-1&&text.charAt(index) != 'a'){
return 1+Count(text,find);
}
else
return Count(text,find);
}
return 0;
}
Just working the answer out in pseudocode, here's how I'd approach the problem.只需用伪代码找出答案,这就是我解决问题的方法。
Define a helper function as follows:定义一个助手 function 如下:
countHelper(string text, string find, bool previousWasNotA) =
if (length of text < length of find) {
0
} else {
let prefixEqualsFind = if (previousWasNotA and find is a prefix of text) {
0
} else {
1
}
in
prefixEqualsFind + countHelper(text without first character, find, first character of text != 'a')
}
count(string text, string find) = countHelper(text, find, true)
The idea here is that countHelper(text, find, previousWasNotA)
returns the number of occurences of find
in text
, not counting any occurences where find
is directly preceded by an a
, and not counting an occurence of find
at the very beginning of text
if previousWasNotA
is false.这里的想法是
countHelper(text, find, previousWasNotA)
返回find
在text
中出现的次数,不计算find
直接在 a 前面的任何a
,并且不计算在text
开头出现的find
如果previousWasNotA
为假。
In Java, this look like在 Java 中,这看起来像
static int countHelper(String text, String find, bool previousWasNotA) {
if (text.length() < find.length()) {
return 0;
} else {
const int prefixEqualsEnd = previousWasNotA && text.startsWith(find) ? 1 : 0;
return prefixEqualsEnd + countHelper(text.substring(1), find, text.charAt(0) != 'a');
}
}
static int count(String text, String find) {
return countHelper(text, find, true);
}
Note that this doesn't work for the case of find = ""
.请注意,这不适用于
find = ""
的情况。 But in that case, it's not clear the problem even has an answer at all, since we can put infinitely many ""s together to make a single "" and hence infinitely many ""s are contained in any string at all.但是在那种情况下,这个问题甚至根本没有答案,因为我们可以将无限多的“”放在一起形成一个“”,因此任何字符串中都包含无限多的“”。
Also note that this is not an asymptotically optimal algorithm.另请注意,这不是渐近最优算法。 For that, you'll want to use the KMP algorithm .
为此,您需要使用KMP 算法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.