Java LCS lcs(String a, String b)

Here you can find the source of lcs(String a, String b)

Description

lcs

License

Apache License

Declaration

private static int lcs(String a, String b) 

Method Source Code

//package com.java2s;
/*// w  w  w.j  a va2  s  .  c om
 * #%L
 * Simmetrics Core
 * %%
 * Copyright (C) 2014 - 2016 Simmetrics Authors
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 * #L%
 */

public class Main {
    private static int lcs(String a, String b) {

        final int m = a.length();
        final int n = b.length();

        int[] v0 = new int[n];
        int[] v1 = new int[n];

        int z = 0;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (a.charAt(i) == b.charAt(j)) {
                    if (i == 0 || j == 0) {
                        v1[j] = 1;
                    } else {
                        v1[j] = v0[j - 1] + 1;
                    }
                    if (v1[j] > z) {
                        z = v1[j];
                    }
                } else {
                    v1[j] = 0;
                }
            }
            final int[] swap = v0;
            v0 = v1;
            v1 = swap;
        }
        return z;
    }
}

Related

  1. lcs(char[] A, char[] B)
  2. lcs(char[] inputString1, char[] inputString2, int length1, int length2)
  3. LCS(String A, String B)
  4. LCS(String a, String b)
  5. lcs(String a, String b)
  6. lcs(String arg0, String arg1)
  7. LCS(String input1, String input2)
  8. lcs(String s1, int s1min, int s1max, String s2, int s2min, int s2max)
  9. lcs(String s1, String s2)