我遇到了一个问题语句,以找到给定的两个子字符串之间的所有常用子字符串,以便在每种情况下都必须打印最长的子字符串.问题说明如下:
Write a program to find the common substrings between the two given strings. However, do not include substrings that are contained within longer common substrings.
For example, given the input strings eatsleepnightxyz and eatsleepabcxyz, the results should be:
eatsleep (due to eatsleepnightxyz eatsleepabcxyz)
xyz (due to eatsleepnightxyz eatsleepabcxyz)
a (due to eatsleepnightxyz eatsleepabcxyz)
t (due to eatsleepnightxyz eatsleepabcxyz)
However, the result set should not include e from
eatsleepnightxyz eatsleepabcxyz, because both es are already contained in the eatsleep mentioned above. Nor should you include ea, eat, ats, etc., as those are also all covered by eatsleep.
In this, you don’t have to make use of String utility methods like: contains, indexOf, StringTokenizer, split and replace.
我的算法如下:我以强力开始,当我提高我的基本理解时,会切换到更优化的解决方案.
For String S1:
Find all the substrings of S1 of all the lengths
While doing so: Check if it is also a substring of
S2.
试图弄清我的方法的时间复杂度.
让两个给定的字符串是n1-String和n2-String
> S1的子串数明显为n1(n1 1)/ 2.
>但是我们必须找到S1的子串的平均长度.
>假设是m我们会分别找到m.
>时间复杂性来检查一个m-String是否是一个子串
n-String是O(n * m).
>现在我们检查每个m-String是一个S2的子串,
这是一个n2-String.
>如上所述,这是一个O(n2 m)算法.
>然后整体算法所需的时间是
> Tn =(S1中的子串数)*(字符比较过程的平均子字符串长度时间)
通过进行某些计算,我得出结论
时间复杂度为O(n3 m2)
>现在,我们的工作是用n1来找到m.
试图用n1来找到m.
Tn =(n)(1)(n-1)(2)(n-2)(3)…..(2)(n-1)(1)(n)
其中Tn是所有子串的长度之和.
平均值是将此总和除以产生的子字符串数.
这只是一个求和和除法问题,其解决方案如下O(n)
因此…
我的算法的运行时间是O(n ^ 5).
考虑到这一点,我写了以下代码:
package pack.common.substrings;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
public class FindCommon2 {
public static final Set commonSubstrings = new LinkedHashSet();
public static void main(String[] args) {
printCommonSubstrings("neerajisgreat", "neerajisnotgreat");
System.out.println(commonSubstrings);
}
public static void printCommonSubstrings(String s1, String s2) {
for (int i = 0; i < s1.length();) {
List list = new ArrayList();
for (int j = i; j < s1.length(); j++) {
String subStr = s1.substring(i, j + 1);
if (isSubstring(subStr, s2)) {
list.add(subStr);
}
}
if (!list.isEmpty()) {
String s = list.get(list.size() - 1);
commonSubstrings.add(s);
i += s.length();
}
}
}
public static boolean isSubstring(String s1, String s2) {
boolean isSubstring = true;
int strLen = s2.length();
int strToCheckLen = s1.length();
if (strToCheckLen > strLen) {
isSubstring = false;
} else {
for (int i = 0; i <= (strLen - strToCheckLen); i++) {
int index = i;
int startingIndex = i;
for (int j = 0; j < strToCheckLen; j++) {
if (!(s1.charAt(j) == s2.charAt(index))) {
break;
} else {
index++;
}
}
if ((index - startingIndex) < strToCheckLen) {
isSubstring = false;
} else {
isSubstring = true;
break;
}
}
}
return isSubstring;
}
}
我的代码说明:
printCommonSubstrings: Finds all the substrings of S1 and
checks if it is also a substring of
S2.
isSubstring : As the name suggests, it checks if the given string
is a substring of the other string.
问题:给出投入
S1 = “neerajisgreat”;
S2 = “neerajisnotgreat”
S3 = “rajeatneerajisnotgreat”
在S1和S2情况下,输出应为:neerajis和伟大
但是在S1和S3的情况下,输出应该是:
neerajis,raj,伟大的,吃,但我仍然是neerajis和伟大的产出.我需要弄清楚这一点.
我应该如何设计我的代码?