My AC Java Solution


  • 0
    N
    public String longestCommonPrefix(String[] strs) {
        if (strs.length == 0) return "";
        StringBuilder sb = new StringBuilder();
        int index = 0;
        while (index < strs[0].length()) {
            for (int i = 1; i < strs.length; i++) {
                if (strs[i].length() < index + 1 || strs[i].charAt(index) != strs[0].charAt(index)) return sb.toString();
            }
            sb.append(strs[0].charAt(index));
            index++;
        }
        return sb.toString();
    }

Log in to reply
 

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