Java solution (5 loc)

  • 1
        if (strs.length != 0)
            for (int i = 0, length = strs[0].length() + 1; i < length; ++i)
                for (String str : strs)
                    if (str.length() == i || str.charAt(i) != strs[0].charAt(i)) return str.substring(0, i);
        return "";

    Although it's not very readable.

  • 0

    the last return statement should be return strs[0]

  • 0

    that would fail with NPE. The currly braces are missing so it is not readable, but if en empty array is on the input it'll fail

Log in to reply

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