Longest Common Prefix

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

Solution:
public class Solution {
    public String longestCommonPrefix(String[] strs) {
        if(strs == null || strs.length == 0) return "";
        String output = strs[0];
        for(String s: strs){
            while(!s.substring(0,Math.min(output.length(),s.length())).equals(output)){
                output = output.substring(0,Math.max(output.length()-1,0));
                if(output.equals("")) return "";
            }
        }
        return output;
    }
}

results matching ""

    No results matching ""