Using JAVA stream, but not efficiently because of sequential merge of combiners


  • 0
    F
    public String longestCommonPrefix(String[] strs) {
        String result = Arrays.stream(strs).reduce(((s, s2) -> {
            int i;
            for (i = 0; i < Math.min(s.length(), s2.length()); i++) {
                if (s.charAt(i) != s2.charAt(i)) {
                    break;
                }
            }
            return s.substring(0, i);
        })).orElse("");
        return result;
    }

Log in to reply
 

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