简体   繁体   English

如何检查我在字符串中查找的单词的前一个字母是否存在? 递归

[英]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)返回findtext中出现的次数,不计算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.

相关问题 如何检查单词是否包含一个字母或一组字母? - How do I check to see if a word contains a letter or group of letters? 我如何制作读取一个单词并告诉我是否以字母 a 开头的 Java 代码? 我搞不清楚了 - how do i make java code that reads a word and tells me if is starts with the letter a or not? i am lost 如何检查 Java 字符串是否至少包含一个大写字母、小写字母和数字? - How do I check if a Java String contains at least one capital letter, lowercase letter, and number? 如何在字符串中搜索短语而不多次查找短语中的单词 - How do I search for a phrase in a string without looking for the word in the phrase multiple times 我如何给名为word的数组的第一个值赋值? - how do i give a the first letter of an array called word a value? 如何在每个单词中找到第一个字母? - How do I find the first letter in each word? 如何检查仅包含字母(大写和小写)以及至少一个数字的字符串 - How do i check string to contain only letter(Uppercase & Lowercase) and at least one number only 如何检查String是否包含多次相同的字母? - How do I check if a String contains the same letter more than once? 如何选择前面的兄弟 Xpath - How do I select preceding sibling Xpath 如何检查类名中是否存在单词? - How do I check if a word is present in the classname?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM