Edit distance between 2 strings
However, i would like to optimize the loop condition.
  Currently 3 operations are done. What can I do to speed it up?


Instead of checking for every position if it's within the bounds of both strings,
you could save the lowest bound using Math.min and use that as the loop condition.
That will cut down one comparison.


  Can anyone suggest a more elegant solution?


The last condition checking destLength !=  sourceLength can be omitted,
you can simply add the value of Math.abs because it will give 0 if the lengths are the same.
The result will be shorter code, which may be slightly more readable.

To make the code look more elegant,
it would be good to follow the common coding style.
For example instead of this:


    for ( int i=0 ;i &lt; sourceLength &amp;&amp; i &lt; destLength ; i++){



Write like this:

    for (int i = 0; i &lt; sourceLength &amp;&amp; i &lt; destLength; i++) {


I use my IDE to correct the formatting. It's a matter of a few key strokes.

It goes without saying that you should remove auto-generated lines like this:


    // TODO Auto-generated method stub



The revised implementation incorporating the above points (and a point from @user1016274 too):

public static int getEditDistance(String sourceString, String destinationString) {
    if (sourceString == null || destinationString == null){
        throw new IllegalArgumentException("String cannot be null");
    }

    int sourceLength = sourceString.length();
    int destLength = destinationString.length();
    int len = Math.min(sourceLength, destLength);

    int distance = Math.abs(sourceLength - destLength);
    for (int i = 0; i &lt; len; ++i) {
        if (sourceString.charAt(i) != destinationString.charAt(i)) {
            ++distance;
        }
    }

    return distance;
}


I would also recommend to add some unit tests to verify the algorithm is working:

@Test
public void test_coti_cat() {
    assertEquals(2, EditDistance.getEditDistance("coti", "cat"));
}

@Test
public void test_alpha_beta() {
    assertEquals(5, EditDistance.getEditDistance("alpha", "beta"));
}

@Test
public void test_beta_pedal() {
    assertEquals(3, EditDistance.getEditDistance("beta", "pedal"));
}

@Test
public void test_empty() {
    assertEquals(3, EditDistance.getEditDistance("", "123"));
}