10 lines java solution


  • 0
    Y
        if (strs.length == 0 || strs == null)   return "";
        StringBuilder result = new StringBuilder().append(strs[0]);
        for (int i = 0; i < strs.length; i++) {
            for (int j = 0; j < result.length(); j++) {
                if (j >= strs[i].length() || strs[i].charAt(j) != result.charAt(j)) {
                    result.delete(j, result.length());
                    break;
                }
            }
        }
        return result.toString();
    

    If you have better solutions, pls share with me. Thx!


Log in to reply
 

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