sfrizvi6
BAN USERUsing a modification of Longest Common Subsequence algorithm we can solve it the following way:
public static int lcs(String str1, String str2, int m, int n, int length) {
if (length == 2) return length;
if (m == 0 || n == 0) return 0;
if (str1.charAt(m-1) == str2.charAt(n-1)) return lcs(str1, str2, m-1, n-1, length+1);
else return max(lcs(str1, str2, m, n-1, length), lcs(str1, str2, m-1, n, length));
}
public static int max(int a, int b) {
return (a > b)? a : b;
}
public static boolean repeatingSubsequence(String str) {
String str1 = "";
String str2 = "";
int i = 2;
while (i < str.length() - 1) {
str1 = str.substring(0, i);
str2 = str.substring(i);
if (lcs(str1, str2, str1.length(), str2.length(), 0) >= 2) {
return true;
}
i++;
}
return false;
}
Using a modification of Longest Common Subsequence algorithm we can solve it the following way:
- sfrizvi6 December 03, 2014