Longest common subsequence difference
<p>I have a program that I am writing in Java and have to do 2 things, find the longest common sub-sequence and align the common characters. The LCS works just fine but the align part just loops away or do nothing.</p>

<p>I try to do this algorithm which I found on Wikipedia</p>

<pre><code>function printDiff(C[0..m,0..n], X[1..m], Y[1..n], i, j)
if i &gt; 0 and j &gt; 0 and X[i] = Y[j]
    printDiff(C, X, Y, i-1, j-1)
    print "  " + X[i]
else if j &gt; 0 and (i = 0 or C[i,j-1] ≥ C[i-1,j])
    printDiff(C, X, Y, i, j-1)
    print "+ " + Y[j]
else if i &gt; 0 and (j = 0 or C[i,j-1] &lt; C[i-1,j])
    printDiff(C, X, Y, i-1, j)
    print "- " + X[i]
else
    print ""
</code></pre>

<p>Here is the code I wrote (I removed the LCS part)</p>

<pre><code>static char[] input1 = "ABCDE".toCharArray();
static char[] input2 = "ACDC".toCharArray();
static int M = input1.length;
static int N = input2.length;
static int[][] opt = new int[M + 1][N + 1];

public static void printDiff(int opt[][], char input1[], char input2[]) {
    int i = 0, j = 0;
    while (i &lt; input1.length &amp;&amp; j &lt; input2.length) {
    if (i &gt; 0 &amp;&amp; j &gt; 0 &amp;&amp; input1[i] == input2[j]) {
        System.out.print("  " + input1[i]);
        i++;
        j++;
    } else if (j &gt; 0 &amp;&amp; (i == 0 || opt[i][j - 1] &gt;= opt[i - 1][j])) {
        System.out.print("+ " + input2[j]);
        j++;
    } else if (i &gt; 0 &amp;&amp; (j == 0 || opt[i][j - 1] &lt; opt[i - 1][j])) {
        System.out.print("- " + input1[i]);
        i++;
    } else {
        System.out.print("");

    }
    }
}
</code></pre>
