[英]How to find common suffix using recursion in java
So I have seen on Stack Overflow that common suffix can be derived using methods which do not use recursion. 因此,我在Stack Overflow上看到,可以使用不使用递归的方法派生通用后缀。 I was curious if i could achieve the same with recursion.
我很好奇能否通过递归实现相同的目标。 Tried with below code but getting java.lang.StackOverflowError.
尝试使用下面的代码,但得到java.lang.StackOverflowError。 Can someone help?
有人可以帮忙吗?
public static String commonSuffixWithRecursion(String s1, String s2) {
if(s1.length()==0 || s2.length()==0) {
return "Strings are empty";
}
if(s1.charAt(s1.length()-1)==s2.charAt(s2.length()-1)) {
return s1.length()-1 + commonSuffixWithRecursion(s1,s2);
}
return "No common Suffix";
}
It should return empty string when there is not common suffix. 没有通用后缀时,应返回空字符串。
You can use subString
to get the input for next round 您可以使用
subString
获取下一轮的输入
Current char should be appended after the result of next rount 目前焦炭下一步应该rount的结果后追加
Sample code: 样例代码:
public static String commonSuffixWithRecursion(String s1, String s2) {
if (s1.length() == 0 || s2.length() == 0) {
return "";
}
if (s1.charAt(s1.length() - 1) == s2.charAt(s2.length() - 1)) {
String nextS1 = s1.substring(0, s1.length() - 1);
String nextS2 = s2.substring(0, s2.length() - 1);
return commonSuffixWithRecursion(nextS1, nextS2 ) + s1.charAt(s1.length() - 1);
}
return "";
}
Test case: 测试用例:
public static void main(String[] args) {
System.out.println(commonSuffixWithRecursion("dbc", "abc")); // bc
System.out.println(commonSuffixWithRecursion("a", "a")); // a
System.out.println(commonSuffixWithRecursion("ab", "b")); // b
System.out.println(commonSuffixWithRecursion("a", "b")); // empty
}
One simple way to do it recursively is 递归执行的一种简单方法是
public static String commonSuffixWithRecursion(String x, String y, int m, int n) {
if(m <= 0 || n <= 0)
return "";
if (x.charAt(m-1) == y.charAt(n-1))
return commonSuffixWithRecursion(x, y, m-1, n-1) + x.charAt(m-1);
else
return "";
}
Here m=x.length() and n=y.length()
这里
m=x.length() and n=y.length()
Here is another option using an incremented suffix size: 这是使用增加的后缀大小的另一个选项:
public static String commonSuffixWithRecursion(String s1, String s2, int suffixSize) {
int i1 = s1.length() - suffixSize;
int i2 = s2.length() - suffixSize;
if (i1 > -1 && i2 > -1 && s1.charAt(i1) == s2.charAt(i2)) {
return commonSuffixWithRecursion(s1, s2, suffixSize + 1);
}
return s1.substring(i1 + 1);
}
Here are some test cases: 以下是一些测试案例:
public static void main(String[] args) {
System.out.println(commonSuffixWithRecursion("dbc", "abc", 1)); // bc
System.out.println(commonSuffixWithRecursion("a", "a", 1)); // a
System.out.println(commonSuffixWithRecursion("ab", "b", 1)); // b
System.out.println(commonSuffixWithRecursion("a", "b", 1)); // empty
System.out.println(commonSuffixWithRecursion("a", "bc", 1)); // empty
System.out.println(commonSuffixWithRecursion("ac", "bc", 1)); // c
System.out.println(commonSuffixWithRecursion("ac", "bcd", 1)); // empty
System.out.println(commonSuffixWithRecursion("", "bcd", 1)); // empty
System.out.println(commonSuffixWithRecursion("bcd", "bcd", 1)); // bcd
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.