Java Array Common Prefix longestCommonPrefix2(String[] strs)

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

Description

Vertical scanning

License

Open Source License

Parameter

Parameter Description
strs a parameter

Declaration

public static String longestCommonPrefix2(String[] strs) 

Method Source Code

//package com.java2s;

public class Main {
    /**//from  ww w  .  j  a v a  2s.c  om
     * Vertical scanning
     *
     * @param strs
     * @return
     */
    public static String longestCommonPrefix2(String[] strs) {
        if (strs == null || strs.length == 0) {
            return "";
        }
        for (int i = 0; i < strs[0].length(); i++) {
            char c = strs[0].charAt(i);
            for (int j = 1; j < strs.length; j++) {
                if (i == strs[j].length() || c != strs[j].charAt(i)) {
                    return strs[0].substring(0, i);
                }
            }
        }
        return strs[0];
    }
}

Related

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