Java Array Common Prefix longestCommonPrefix(String[] strArray)

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

Description

longest Common Prefix

License

Open Source License

Declaration

public static String longestCommonPrefix(String[] strArray) 

Method Source Code

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

public class Main {
    public static String longestCommonPrefix(String[] strArray) {
        if (strArray.length == 0)
            return "";
        if (strArray.length == 1)
            return strArray[0];
        int maxLength = Integer.MAX_VALUE;
        for (int i = 0; i < strArray.length; i++) {
            maxLength = Math.min(strArray[i].length(), maxLength);
        }/*from www.jav a  2 s. c o  m*/
        if (maxLength == 0)
            return "";
        for (int i = 0; i < maxLength; i++) {
            for (int j = 0; j < strArray.length - 1; j++) {
                if (strArray[j].charAt(i) != strArray[j + 1].charAt(i)) {
                    return strArray[j].substring(0, i);
                }
            }
        }
        return strArray[0].substring(0, maxLength);
    }
}

Related

  1. longestCommonPrefix(String a, String b)
  2. longestCommonPrefix(String one, String two)
  3. longestCommonPrefix(String s1, String s2)
  4. longestCommonPrefix(String str1, String str2)
  5. longestCommonPrefix(String... strs)
  6. longestCommonPrefix(String[] strs)
  7. longestCommonPrefix(String[] strs)
  8. longestCommonPrefix(String[] strs)
  9. longestCommonPrefix(String[] strs)