Java Array Compare longestCommonPrefix(String[] stringArray)

Here you can find the source of longestCommonPrefix(String[] stringArray)

Description

Returns the longest common prefix for the specified strings.

License

BSD License

Parameter

Parameter Description
stringArray an array of strings.

Return

the longest common prefix for the specified strings.

Declaration

public static final String longestCommonPrefix(String[] stringArray) 

Method Source Code

//package com.java2s;
// Licensed under the terms of the New BSD License. Please see associated LICENSE file for terms.

import java.util.Arrays;

import java.util.List;

public class Main {
    /**/*from  w w  w  .  j  a va  2s.  co  m*/
     * Returns the longest common prefix for the specified strings
     * 
     * @param s the first string.
     * @param t the second string.
     * @return the longest common prefix for the specified strings
     */
    public static final String longestCommonPrefix(String s, String t) {
        int n = Math.min(s.length(), t.length());
        int i;
        for (i = 0; i < n; ++i) {
            if (s.charAt(i) != t.charAt(i)) {
                break;
            }
        }
        return s.substring(0, i);
    }

    /**
     * Returns the longest common prefix for the specified strings.
     * 
     * @param stringArray an array of strings.
     * @return the longest common prefix for the specified strings.
     */
    public static final String longestCommonPrefix(String[] stringArray) {
        return longestCommonPrefix(Arrays.asList(stringArray));
    }

    /**
     * Returns the longest common prefix for the specified strings.
     * 
     * @param stringList a list of strings.
     * @return the longest common prefix for the specified strings.
     */
    public static final String longestCommonPrefix(List<String> stringList) {
        final int n = stringList.size();
        if (n < 1) {
            throw new IllegalArgumentException();
        }
        String lcpString = stringList.get(0);
        for (String string : stringList) {
            lcpString = longestCommonPrefix(lcpString, string);
        }
        return lcpString;
    }
}

Related

  1. arrayCompareLex(byte[] a, byte[] b)
  2. arrayContentsEq(Object[] a1, Object[] a2)
  3. createStainMask(float[] redOD, float[] greenOD, float[] blueOD, double stainThreshold, boolean excludeGray, boolean excludeUncommonColors, boolean[] mask)
  4. getLongestCommonSubsequence(int[] a, int[] b)
  5. getRelativeSegments(String[] targetPath, int commonSegments, int discardedSegments)
  6. longestCommonSubsequence(E[] s1, E[] s2)
  7. longestCommonSubsequenceAlternate(int[] input)
  8. longestCommonSubstring(String s[])