Java Array Common Prefix longestCommonPath(String... paths)

Here you can find the source of longestCommonPath(String... paths)

Description

Returns the longest common path of a group of path strings.

License

Open Source License

Parameter

Parameter Description
paths the path strings

Return

the longest common path

Declaration

public static String longestCommonPath(String... paths) 

Method Source Code

//package com.java2s;
/*// w  w w .ja  va  2 s  .c  o  m
 * Black Duck Software Suite SDK
 * Copyright (C) 2015  Black Duck Software, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

public class Main {
    /**
     * Returns the longest common path of a group of path strings.
     *
     * <p>
     * Expects a path separator of {@code '/'}. Result will not include a trailing {@code '/'} unless all input path
     * strings are equal and have a trailing {@code '/'}. Returns an empty string if there are no input path strings.
     * </p>
     *
     * @param paths
     *            the path strings
     * @return the longest common path
     */
    public static String longestCommonPath(String... paths) {
        if (paths.length == 0) {
            return "";
        }

        if (paths.length == 1) {
            return paths[0];
        }

        String prefix = longestCommonPrefix(paths);
        int prefixLength = prefix.length();
        boolean mustBacktrack = false;

        for (String path : paths) {
            if ((path.length() > prefixLength) && (path.charAt(prefixLength) != '/')) {
                mustBacktrack = true;
                break;
            }
        }

        String path;

        if (mustBacktrack) {
            int end = prefix.lastIndexOf('/');
            path = end > 0 ? prefix.substring(0, end) : "";
        } else {
            path = prefix;
        }

        return path;
    }

    /**
     * Returns the longest common prefix of a group of strings.
     *
     * <p>
     * Returns an empty string if there are no input strings.
     * </p>
     *
     * @param strs
     *            the strings
     * @return the longest common prefix
     */
    private static String longestCommonPrefix(String... strs) {
        if (strs.length == 0) {
            return "";
        }

        if (strs.length == 1) {
            return strs[0];
        }

        int end = 0;

        charLoop: do {
            char c = strs[0].charAt(end);

            for (int i = 1; i < strs.length; i++) {
                if ((strs[i].length() <= end) || (strs[i].charAt(end) != c)) {
                    break charLoop;
                }
            }

            end++;
        } while (true);

        return strs[0].substring(0, end);
    }
}

Related

  1. longestCommonContiguousSubstring(String s, String t)
  2. longestCommonPrefix(CharSequence str1, CharSequence str2)
  3. longestCommonPrefix(String a, String b)
  4. longestCommonPrefix(String one, String two)
  5. longestCommonPrefix(String s1, String s2)