[英]Given two Strings, determine if 1 is a substring of the other recursively
I'm running through some practice problems, one of which is to take two Strings and determine if one is a substring of the other. 我正在经历一些练习问题,其中一个是拿两个字符串并确定一个是否是另一个的子串。 I wrote my first solution:
我写了第一个解决方案:
public static boolean isSubstring(String s1, String s2){
String longer = s1.length() > s2.length() ? s1 : s2;
String shorter = s1.length() < s2.length() ? s1 : s2;
String substring;
for(int i = 0; i < longer.length() && i + shorter.length() <= longer.length(); i++){
substring = longer.substring(i, i + shorter.length());
if(substring.equals(shorter))
return true;
}
return false;
}
And then I remembered that Java has a contains method for String class, and the solution is trivial at that point. 然后我记得Java有一个String类的包含方法,此时解决方案很简单。 :
:
public static boolean isSubstringUsingContains(String s1, String s2){
String longer = s1.length() > s2.length() ? s1 : s2;
String shorter = s1.length() < s2.length() ? s1 : s2;
return longer.contains(shorter);
}
Since I'm practicing and I'm not very good at recursion, I thought I should try to solve this recursively, and I have not been able to do so correctly. 由于我正在练习并且我不太擅长递归,我认为我应该尝试递归地解决这个问题,而且我无法正确地解决这个问题。 How can I solve this with recursion, and are there any pros/cons for using recursion here as opposed to the above solutions.
我如何通过递归来解决这个问题,并且在这里使用递归有任何优点/缺点,而不是上述解决方案。 Here is my failed attempt at a recursive solution for this problem:
这是我尝试递归解决此问题的失败:
public static boolean isSubstringRecursive(String s1, String s2,int i){
if(s1.length() == 0 || s2.length() == 0)
return false;
String longer = s1.length() > s2.length() ? s1 : s2;
String shorter = s1.length() < s2.length() ? s1 : s2;
if(longer.equals(shorter))
return true;
return isSubstringRecursive(longer.substring(i,i + shorter.length()),shorter,i + 1);
}
EDIT 编辑
Test Cases: String A = "Batman" String B = "atm" RESULT: True 测试用例:String A =“Batman”String B =“atm”RESULT:True
String A = "Handstand" String B = "stand" RESULT: True 字符串A =“手倒立”字符串B =“站立”结果:真
String A = "Hotsauce" String B = "ecu" RESULT: False 字符串A =“Hotsauce”字符串B =“ecu”结果:错误
I am making minimal changes to your routine to make it work. 我正在对您的日常工作进行微小的更改以使其正常工作。 see my comments in the code for explanation.
请参阅我在代码中的注释以获得解释。
/* Adding an extra argument to store the longest value constantly across multiple recursions. */
public static boolean isSubstringRecursive(String longString, String s1, String s2,int i){
if(s1.length() == 0 || s2.length() == 0)
return false;
String longer = s1.length() >= s2.length() ? s1 : s2;
String shorter = s1.length() < s2.length() ? s1 : s2;
/* set the longString in the first iteration only */
if(i==0)longString=longer;
if(longer.equals(shorter))
return true;
/* To prevent substring to go out of boounds */
if(i+ shorter.length() > longString.length()) return false;
/* Use longString for substring since it holds the originally long string */
return isSubstringRecursive(longString, longString.substring(i,i + shorter.length()),shorter,i + 1);
}
This code can be further optimized to a great extent. 该代码可以在很大程度上进一步优化。 But I will leave that part to you for further work.
但我会把这部分留给你进一步的工作。
Note 1 : As you say, you can easily achieve this using the 'contains' method of the String class. 注1 :正如您所说,您可以使用String类的'contains'方法轻松实现此目的。 However, in the solution that you have demonstrated for
contains
, you don't really have to check the string lengths. 但是,在针对
contains
演示的解决方案contains
,您实际上不必检查字符串长度。 See the following: 请参阅以下内容:
return s1.contains(s2) || s2.contains(s1);
Note 2 : You can also use the indexOf
of the String class, to do the same. 注意2 :您也可以使用String类的
indexOf
来执行相同的操作。 See the following: 请参阅以下内容:
return (s1.indexOf(s2) != -1 || s2.indexOf(s1) != -1);
Long story short, there are so many easier options to achieve your objective that there is no need to go for recursion
option. 长话短说,有很多更简单的选择来实现你的目标,没有必要去
recursion
选项。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.