Java Array Common Prefix longestCommonPrefix(String[] strs)

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

Description

longest Common Prefix

License

Open Source License

Declaration

public static String longestCommonPrefix(String[] strs) 

Method Source Code

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

public class Main {
    public static String longestCommonPrefix(String[] strs) {
        if (null == strs || 0 == strs.length)
            return "";
        String prefix = strs[0];/*from  www  .  j a va 2s  .c  o m*/
        for (int i = 0; i < strs.length; i++) {
            String str = strs[i];
            int j = 0;
            for (j = 0; j < prefix.length(); j++) {
                if (j >= str.length() || prefix.charAt(j) != str.charAt(j))
                    break;
            }
            prefix = prefix.substring(0, j);
        }
        return prefix;
    }
}

Related

  1. longestCommonPrefix(String... strs)
  2. longestCommonPrefix(String[] strArray)
  3. longestCommonPrefix(String[] strs)
  4. longestCommonPrefix(String[] strs)
  5. longestCommonPrefix(String[] strs)
  6. longestCommonPrefix(String[] strs)
  7. longestCommonPrefix(String[] strs)
  8. longestCommonPrefix(String[] strs)
  9. longestCommonPrefix(String[] strs, int l, int r)