Java code with 13 lines


  • 94
    D
    public String longestCommonPrefix(String[] strs) {
        if(strs == null || strs.length == 0)    return "";
        String pre = strs[0];
        int i = 1;
        while(i < strs.length){
            while(strs[i].indexOf(pre) != 0)
                pre = pre.substring(0,pre.length()-1);
            i++;
        }
        return pre;
    }

  • 16
    S

    How about the case when there is not common prefix?


  • 7
    M

    When the case that only the last string is different from the other strings, your code will take a long time, with worst case complexity being O(nk^2) where n is the number of strings and k is the average size of string.


  • 0
    C

    After read your code:
    My Python Code:

    def longestCommonPrefix(self, strs):
            strs=strs or ['']
            intersect__str=min(strs,key= lambda x:len(x))
            for innerStr__str in strs:
                while  innerStr__str.find(intersect__str)!=0:
                    intersect__str=intersect__str[:-1]                
            return intersect__str

  • 84

    Since you seem to value the number of lines, why didn't you use a for-loop? It saves three lines.

    public String longestCommonPrefix(String[] strs) {
        if (strs.length == 0) return "";
        String pre = strs[0];
        for (int i = 1; i < strs.length; i++)
            while(strs[i].indexOf(pre) != 0)
                pre = pre.substring(0,pre.length()-1);
        return pre;
    }
    

  • 0

    What about it?


  • 1
    Y

    pre would become "" and after the while loop is over return pre.


  • 4
    B

    My C++ answer:

     class Solution {
        public:
            string longestCommonPrefix(vector<string>& strs) {
                string toReturn;
                if (!strs.size()) return toReturn;
                for (int i = 0; i < strs[0].size(); ++i){
                    char curr = strs[0][i];
                    for (int j = 1; j < strs.size(); ++j){
                        if (i > strs[j].size()-1 || curr != strs[j][i]) return toReturn;
                    }
                    toReturn.push_back(curr);
                }
                return toReturn;
            }
        };

  • -15
    H

    I don't think this works for the test case strs = ["abcdef", "abcd", "abcde"]
    It prints "abcd" instead if "abcde"


  • 0
    W

    you can put this line“pre = pre.substring(0,pre.length()-1)” into for so you can save another line


  • 4
    T

    python version:

    def longestCommonPrefix(self, strs):
            if len(strs) == 0: return ''
            prefix = strs[0]
            for i in xrange(1, len(strs)):
                while strs[i][:len(prefix)] != prefix:
                    prefix = prefix[:-1]
            return prefix

  • 1
    H

    To make you function faster, you can check if pre.length() == 0 and return;


  • 0

    Your code is effectively running!


  • 3
    E

    We don't even need the String pre if we want to save the line ;-)

    public String longestCommonPrefix(String[] strs) {
        if (strs.length == 0) return "";
        for (int i = 1; i < strs.length; i++) 
            while (strs[i].indexOf(strs[0]) != 0) 
                strs[0] = strs[0].substring(0, strs[0].length() - 1);
        return strs[0];
    }

  • 5

    Your code rely on the index of function heavily..... I think it is not the motivation of the judge ......


  • 0
    I

    Fantastic comment!


  • 0
    M

    I agree with RainbowSecret


  • 4
    C

    Nice solution! Although using 'startsWith' as follows instead of 'indexOf' would make it more readable.

    .
    .
    while(!strs[i].startsWith(pre))
    .
    .


  • 0
    B

    What you said is exactly true!!!


  • 0
    C

    That is why it is the longest common prefix among other elements.


Log in to reply
 

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