patternjavaMinor
Two string comparison
Viewed 0 times
twostringcomparison
Problem
I wrote a function that finds the difference between two strings. This only works given that the difference is continuous.
Beyond doubt this already exists, I am recreating this for educational purposes. I would like feedback about my performance and efficiency.
Examples of this method:
Beyond doubt this already exists, I am recreating this for educational purposes. I would like feedback about my performance and efficiency.
Examples of this method:
String difference = Difference("Hello", "Hello world!");
//difference would be " world!"
String difference = Difference("Removing text", "Removi");
//difference would be "ng text"
String difference = Difference("same size", "SAME size");
//difference would be "SAME"public static String Difference(String string1, String string2)
{
int size = string2.length() - string1.length();
if(size 0)
for(int i = start; i string2.length()) ? string1.length() : string2.length();
for(int i = 0; i string2.length()) ? string1.length() : string2.length();
int end = maxSize;
for(int i = start; i < maxSize; ++i)
if(string1.charAt(i) != string2.charAt(i))
end = i + 1;
return end;
}Solution
Conventions
In java, methods begin with a lowercase letter.
Design
Methods should be private unless they are required by clients. In this case, only the
Implementation
There are a few things to look at here. First, your code is made much more complex by swapping around the two inputs based on size. It would be neater if you did the size comparison once, then called a helper method with parameters
Your two size methods are clunky because they don't return early when they find a (mis)match, and because they run off of the longer string rather than the shorter string.
There may be a slight performance gain to be made if the two strings are frequently equal. In that case only, it may be faster to check if the two strings are
Applying these changes might make the code look something like:
In java, methods begin with a lowercase letter.
Design
Methods should be private unless they are required by clients. In this case, only the
difference() method should be public.Implementation
There are a few things to look at here. First, your code is made much more complex by swapping around the two inputs based on size. It would be neater if you did the size comparison once, then called a helper method with parameters
shorterString and longerString.Your two size methods are clunky because they don't return early when they find a (mis)match, and because they run off of the longer string rather than the shorter string.
There may be a slight performance gain to be made if the two strings are frequently equal. In that case only, it may be faster to check if the two strings are
equals() and return -1 from differenceStartIndex() right away. In your particular case, I highly doubt this check would be anything more than clutter.Applying these changes might make the code look something like:
public final class Difference {
public static String difference(final String string1, final String string2) {
if (string1.length() <= string2.length()) {
return differenceHelper(string1, string2);
}
return differenceHelper(string2, string1);
}
private static String differenceHelper(final String shorterString, final String longerString) {
final int start = differenceStartIndex(shorterString, longerString);
if (start < 0) {
return "";
}
final int end = differenceEndIndex(shorterString, longerString, start);
return longerString.substring(start, end);
}
private static int differenceStartIndex(final String shorterString, final String longerString) {
for (int i = 0; i < shorterString.length(); i++) {
if (shorterString.charAt(i) != longerString.charAt(i)) {
return i;
}
}
if (shorterString.length() == longerString.length()) {
return -1;
}
return shorterString.length();
}
private static int differenceEndIndex(final String shorterString, final String longerString, final int startIndex) {
for (int i = startIndex; i < shorterString.length(); i++) {
if (shorterString.charAt(i) == longerString.charAt(i)) {
return i + 1;
}
}
return longerString.length();
}
public static void main(final String[] argv) {
System.out.println(Difference.difference("Hello", "Hello world!"));
System.out.println(Difference.difference("Removing text", "Removi"));
System.out.println(Difference.difference("same size", "SAME size"));
}
}Code Snippets
public final class Difference {
public static String difference(final String string1, final String string2) {
if (string1.length() <= string2.length()) {
return differenceHelper(string1, string2);
}
return differenceHelper(string2, string1);
}
private static String differenceHelper(final String shorterString, final String longerString) {
final int start = differenceStartIndex(shorterString, longerString);
if (start < 0) {
return "";
}
final int end = differenceEndIndex(shorterString, longerString, start);
return longerString.substring(start, end);
}
private static int differenceStartIndex(final String shorterString, final String longerString) {
for (int i = 0; i < shorterString.length(); i++) {
if (shorterString.charAt(i) != longerString.charAt(i)) {
return i;
}
}
if (shorterString.length() == longerString.length()) {
return -1;
}
return shorterString.length();
}
private static int differenceEndIndex(final String shorterString, final String longerString, final int startIndex) {
for (int i = startIndex; i < shorterString.length(); i++) {
if (shorterString.charAt(i) == longerString.charAt(i)) {
return i + 1;
}
}
return longerString.length();
}
public static void main(final String[] argv) {
System.out.println(Difference.difference("Hello", "Hello world!"));
System.out.println(Difference.difference("Removing text", "Removi"));
System.out.println(Difference.difference("same size", "SAME size"));
}
}Context
StackExchange Code Review Q#108283, answer score: 2
Revisions (0)
No revisions yet.