« Back

Leetcode 14 Longest Common Prefix.

Leetcode 14 Longest Common Prefix

Write a function to find the longest common prefix string amongst an array of strings.

Firstly, I misunderstand the problem, the problem gives you several strings, and we should find the longest prefix among these strings.

public class Solution {
    public String longestCommonPrefix(String[] strs) {
            return "";
        String pre=strs[0];

        for(int i=1;i < strs.length;i++){
            while(strs[i].indexOf(pre) !=0){

        return pre;
comments powered by Disqus