Merge two Strings in java with overlap and with allowed difference

H

Hong Duan

Guest
I want to 'Merge' two Strings, see the code:

public class Test {

public static void main(String[] args) {
printAllPossibleMergedString("abcdefdefd", "defdefdefghi");
}

public static void printAllPossibleMergedString(String a, String b) {
System.out.println(a + b);
int maxOverlap = Math.min(a.length(), b.length());
for (int i = 1; i <= maxOverlap; i++) {
if (b.startsWith(a.substring(a.length() - i, a.length()))) {
System.out.println(a + b.substring(i));
}
}
}
}


output:

abcdefdefddefdefdefghi
abcdefdefdefdefdefghi
abcdefdefdefdefghi
abcdefdefdefghi


Now I want to this method become 'Fault Tolerance' (wish you can understand), something like this:

void printAllPossibleMergedString(String a, String b, int tolerance);


So if I do:

// 'x' means a wrong character
printAllPossibleMergedString("abcdefdefx", "defdefdefghi", 1);


will output:

abcdefdefxdefdefdefghi
abcdefdefdefdefghi
abcdefdefdefghi


while:

printAllPossibleMergedString("abcdefdefxx", "defdefdefghi", 1);


only output:

abcdefdefxxdefdefdefghi


and of course:

printAllPossibleMergedString("abcdefdefxx", "defdefdefghi", 2);


will output:

abcdefdefxdefdefdefghi
abcdefdefdefdefghi
abcdefdefdefghi


So how to implement this efficiently?

Thanks.

Continue reading...
 
Top