longest-common-prefix


/ **
* Write a function to find the longest common prefix string array.
* If there is no common prefix, returns an empty string "."
* Note: All input contains only lowercase letters az.
* /

/ ** 
 * Write a function to find the longest common prefix string array. 
 * If there is no common prefix, returns an empty string "." 
 * Note: All input contains only lowercase letters az. 
 * / 

Public class Main53 { 
    public static void main (String [] args) { 
        String [] = {} STRs; 
        System.out.println (Main53.longestCommonPrefix (STRs)); 
    } 

    public static String longestCommonPrefix (String [] STRs) { 
        IF (STRs strs.length == null || <. 1) { 
            return ""; 
        } 
        String Result = STRs [0]; 
        for (int I =. 1; I <strs.length; I ++) { 
            IF (STRs [! I] .startsWith (Result)) { 
                Result result.substring = (0, result.length () -. 1); 
                i--;
                // new string starts with the entire result, then continue the cycle continues 
                // new string does not begin with the entire result, then the result will be the last cut the string and compare again. 
            } 
        } 
        Return Result; 
    } 
}

  

Guess you like

Origin www.cnblogs.com/strive-19970713/p/11356321.html
Recommended