Java Array Common Prefix longestCommonPrefix1(String[] strs)

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

Description

Horizontal scanning

License

Open Source License

Parameter

Parameter Description
strs a parameter

Declaration

public static String longestCommonPrefix1(String[] strs) 

Method Source Code

//package com.java2s;

public class Main {
    /**//w ww . j ava  2s .  com
     * Horizontal scanning
     *
     * @param strs
     * @return
     */
    public static String longestCommonPrefix1(String[] strs) {
        if (strs.length == 0) {
            return "";
        }
        String prefix = strs[0];
        for (int i = 0; i < strs.length; i++) {
            while (strs[i].indexOf(prefix) != 0) {
                prefix = prefix.substring(0, prefix.length() - 1);
                if (prefix.isEmpty()) {
                    return "";
                }
            }
        }
        return prefix;
    }
}

Related

  1. longestCommonPrefix(String[] strs)
  2. longestCommonPrefix(String[] strs)
  3. longestCommonPrefix(String[] strs)
  4. longestCommonPrefix(String[] strs)
  5. longestCommonPrefix(String[] strs, int l, int r)
  6. longestCommonPrefix2(String[] strs)
  7. longestCommonPrefix3(String[] strs)
  8. longestCommonPrefix4(String[] strs)
  9. longestCommonSequence(String str1, String str2)