Another accepted JAVA solution, beats 99.77% of solutions


  • 0
    S
    public String longestCommonPrefix(String[] strs) {
            if (strs.length == 0) return "";
            if (strs.length == 1) return strs[0];
            String ans = "";
            int min = Integer.MAX_VALUE;
            for (String s : strs){
                if (s.length() < min){
                    ans = s;
                    min = s.length();
                }
            }
            for (String s: strs){
                if (!s.substring(0, ans.length()).equals(ans)) {
                    int i;
                    for (i = 0; i < ans.length() && s.charAt(i) == ans.charAt(i); i++);
                    if ( i == 0 ) return "";
                    else ans = ans.substring(0,i);
                }
            }
            return ans;
        }
    

    0_1494458813961_Untitled.png


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.