Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

import java.util.List;

public class Main {
    public static <T> int longestCommonPrefix(List<List<T>> paths) {
        int[] lengths = new int[paths.size()];
        int minLength = Integer.MAX_VALUE;
        for (int i = 0; i < paths.size(); ++i) {
            int len = paths.get(i).size();
            lengths[i] = len;
            if (len < minLength)
                minLength = len;
        }

        int longestCommonPrefix = 0;
        while (longestCommonPrefix < minLength) {
            List<T> headPath = paths.get(0);
            for (List<T> path : paths) {
                if (!headPath.get(longestCommonPrefix).equals(path.get(longestCommonPrefix))) {
                    return longestCommonPrefix;
                }
            }
            ++longestCommonPrefix;
        }
        return longestCommonPrefix;
    }
}