[英]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 (full.isEmpty()) return false;
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));
partial
and full
are non-empty, let's utilize String
's indexOf
instance method. partial
和full
都是非空的,让我们使用String
的indexOf
实例方法。 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
如果字符没有在字符串中存在。 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));
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
。 &&
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 >= 0
为false
,则不会计算布尔检查的后半部分(即递归调用),因为false && anyOtherBooleanExpression ==> false
。 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)
所有字符再次调用我们的函数。 firstOccurrence
represents the first location of the first character of partial
in full
, we don't care about the (possible) skipped locations. firstOccurrence
代表的第一个字符的第一个位置partial
中full
,我们不关心(可能)跳过位置。
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.