说我有两个弦,

str1 = "abcdefg"
str2 = "cdefghijkl"


如果用户指定了整数,则说n = 4。

如何确定两个字符串是否重叠4个或更多字符?
在这种情况下,它们将“ cdefg”重叠5以使“ abcdefghijkl”

如果n = 6,则它们将不会重叠,因为“ bcdefg”!=“ cdefgh”

我遇到的问题是,如果重叠超过用户指定的数量。

最佳答案

我遇到的问题是,如果重叠超过用户指定的数量。


这就是循环的开始:从用户重叠定义的位置开始,并不断增加直到(1)检测到重叠或(2)字符串之一中的字符用完。

伪代码如下所示:

int userOverlap = ... // Get user overlap
int minLength = Math.min(stringOne.length(), stringTwo.length());
for (int overlap = userOverlap  ; overlap <= minLength; overlap++) {
    if (testOverlap(stringOne, stringTwo, overlap)) {
        return true;
    }
}
return false;

private static bool testOverlap(String a, String b, int overlap) {
    ... // This is your method that tests for one specific overlap
}

09-28 03:55