Here you can find the source of createMatrix(List
Parameter | Description |
---|---|
source | the source list. |
target | the target list. |
private static int[][] createMatrix(List<String> source, List<String> target)
//package com.java2s; /*//from ww w. j a v a 2 s. c o m * Copyright 2011 Andrej Petras <andrej@ajka-andrej.com>. * * 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. */ import java.util.List; public class Main { /** * Create matrix for the compare algorithms. * * @param source the source list. * @param target the target list. * * @return the matrix. */ private static int[][] createMatrix(List<String> source, List<String> target) { int M = source.size(); int N = target.size(); int[][] result = new int[M + 1][N + 1]; for (int i = M - 1; i >= 0; i--) { for (int j = N - 1; j >= 0; j--) { if (source.get(i).equals(target.get(j))) { result[i][j] = result[i + 1][j + 1] + 1; } else { result[i][j] = Math.max(result[i + 1][j], result[i][j + 1]); } } } return result; } }