Java solution, 6 ms


  • 0
    J
    public String longestCommonPrefix(String[] strs) {
        if (strs.length == 0) return "";
        String first = strs[0];
        int end = first.length();
        for (int i = 1; i < strs.length; i++) {
            int j = 0;
            while (j < Math.min(first.length(), strs[i].length()) && first.charAt(j) == strs[i].charAt(j)) {
                j++;
            }
            end = Math.min(j, end);
        }
        return first.substring(0, end);
    }

Log in to reply
 

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