Welcome to LeetCode Discuss.  Please read the FAQ to help yourself making the best use of Discuss.
Ask a Question
Back to Problem

Welcome to LeetCode Discuss.

This is a place to ask questions related to only OJ problems.

Please read the FAQ to help yourself making the best use of Discuss.

JAVA version in linear time

+1 vote
408 views
 public String longestCommonPrefix(String[] strs) {
        if(strs.length==0) return "";
        String str0 = strs[0];
        if(str0.length()==0) return "";
        int rightMostIndex = str0.length()-1;
        for(int k=1;k<strs.length;k++){
            for(int i=0;i<=rightMostIndex;i++){
                if((i>strs[k].length()-1)||strs[k].charAt(i)!=str0.charAt(i))
                    rightMostIndex=i-1;
            }
        }
        return rightMostIndex>=0?str0.substring(0,rightMostIndex+1):"";
}
closed with the note: Questions about code you've written must describe the specific problem clearly. Please read the FAQ (http://oj.leetcode.com/discuss/faq) for more info.
asked Feb 9 in Longest Common Prefix by JeremyShi1983 (230 points)
closed Feb 9 by Shangrila

What happens if the longest prefix string occurs between strs[1] and strs[2]? The question is not clear whether it means the longest prefix for all the strings or whether it means just between two of the strings.

it applies for all strings.


...