Here you can find the source of lcs(String a, String b)
private static int lcs(String a, String b)
//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; } }