简体   繁体   English

需要帮助查看字符串是否包含使用递归的字符列表

[英]Need help seeing if a string contains a list of characters in order using recursion

My teacher gave me an assignment where there are two strings, a full word, and a substring that contains letters that might be in the full word. 我的老师给了我一个作业,其中有两个字符串,一个完整的单词和一个子字符串,其中包含可能在完整单词中的字母。 The objective is to use recursion to see if the full word contains the letters of the substring in order they are given. 目标是使用递归来查看完整的单词是否包含子字符串的字母,以便给出它们。 here are some examples of what I am talking about: 以下是我所谈论的一些例子:

-"xd", "test" returns false because one or more of the substring letters does not exist in the full word. - “xd”,“test”返回false,因为完整单词中不存在一个或多个子字符串字母。

-"ts", "test" returns true because both letters are found found in the full word. - “ts”,“test”返回true,因为在完整单词中找到了两个字母。

-"trl", "turtle" returns true because all the letters are found in the full word and are found in the correct order. - “trl”,“turtle”返回true,因为所有字母都在完整单词中找到并以正确的顺序找到。

-"tlr", "turtle" returns false because although the all the letters exist in the full word, they were not found in the substring's order. - “tlr”,“turtle”返回false,因为尽管所有字母都存在于完整的单词中,但它们在子字符串的顺序中找不到。

I have tried: 我努力了:

public boolean contains_recursive(String partial, String full) {

    String[] partialArray = partial.split("");

    if(full.contains(partialArray[0])) {
        partialArray = Arrays.copyOfRange(partialArray, 1, partialArray.length);
        String newPartial = "";
        for(String character : partialArray) {
            System.out.println(character);
            newPartial += character;
        }
        contains_recursive(newPartial, full);
    }

    return false;
}

but do not know where to go from there. 但不知道从哪里去。 Thanks for any help. 谢谢你的帮助。

Try this: 尝试这个:

public boolean containsRecursive(String partial, String full) {
  if (partial.isEmpty()) return true;
  if (full.isEmpty()) return false;

  int firstOccurrence = full.indexOf(partial.charAt(0));

  return firstOccurrence >= 0 && containsRecursive(partial.substring(1), full.substring(firstOccurrence + 1));
}

Explanation: 说明:

if (partial.isEmpty()) return true;
  • If there is nothing to look for in the full string, then the "substring" technically exists (ie an empty string is a substring of all strings). 如果在完整字符串中没有要查找的内容,那么“substring”在技术上是存在的(即空字符串是所有字符串的子字符串)。
if (full.isEmpty()) return false;
  • If we've got to this point that means there are characters in the partial string that we're looking for but unfortunately the full string doesn't have any characters to compare to, so those characters in the partial string will definitely not be in full , return false . 如果我们达到这一点意味着我们正在寻找的部分字符串中有字符,但不幸的是, full字符串没有任何要比较的字符,所以partial字符串中的那些字符肯定不会出现在full ,返回false
int firstOccurrence = full.indexOf(partial.charAt(0));
  • We've got this far so both partial and full are non-empty, let's utilize String 's indexOf instance method. 我们有这么远,所以partialfull都是非空的,让我们使用StringindexOf实例方法。 This method will return the location of the first occurrence of the given character or -1 if the character doesn't exist in the string. 此方法将返回给定字符的第一次出现位置-1如果字符没有在字符串中存在。
  • We use partial.charAt(0) because if the first character of partial doesn't exist in full then why should we check the remaining characters in partial ? 我们使用partial.charAt(0)因为如果partial的第一个字符不full那么我们为什么要检查partial的剩余字符? In other words, let's make sure that the first character exists before checking the rest. 换句话说,让我们确保在检查其余字符之前存在第一个字符。
return firstOccurrence >= 0 && containsRecursive(partial.substring(1), full.substring(firstOccurrence + 1));
  • We check firstOccurrence >= 0 because if firstOccurrence is -1 then that means partial 's first character wasn't in full so this should return false . 我们检查firstOccurrence >= 0因为如果firstOccurrence-1那么这意味着partial的第一个字符没有full所以这应该返回false
  • The && is used for short-circuiting. &&用于短路。 In other words, if firstOccurrence >= 0 is false the second half of the boolean check (ie the recursive call) won't be evaluated because false && anyOtherBooleanExpression ==> false . 换句话说,如果firstOccurrence >= 0false ,则不会计算布尔检查的后半部分(即递归调用),因为false && anyOtherBooleanExpression ==> false
  • The second half of boolean check is the recursive call. 布尔检查的后半部分是递归调用。 We only get this far if the first character of partial is in full . 如果partial的第一个字符是full我们只能得到这个。 Now we call our function again by taking all the characters of partial minus its first character or partial.substring(1) . 现在我们通过取partial 减去第一个字符 partial.substring(1)所有字符再次调用我们的函数。
  • Since firstOccurrence represents the first location of the first character of partial in full , we don't care about the (possible) skipped locations. 由于firstOccurrence代表的第一个字符的第一个位置partialfull ,我们不关心(可能)跳过位置。
    • For example, if partial = "mtr" and full = "computer" , m matches at position 2 , we should start checking partial = "tr" within full = "puter" on the next recursive call. 例如,如果partial = "mtr"full = "computer"m匹配位置2 ,我们应该在下一次递归调用中开始检查full = "puter"中的partial = "tr"

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM