Here you can find the source of longestCommonPrefix(String[] strs, int l, int r)
private static String longestCommonPrefix(String[] strs, int l, int r)
//package com.java2s; public class Main { private static String longestCommonPrefix(String[] strs, int l, int r) { if (l == r) { return strs[l]; } else {/* www. ja v a 2 s.com*/ int mid = (l + r) / 2; String lcpLeft = longestCommonPrefix(strs, l, mid); String rcpRight = longestCommonPrefix(strs, mid + 1, r); return commonPrefix(lcpLeft, rcpRight); } } private static String commonPrefix(String left, String right) { int min = Math.min(left.length(), right.length()); for (int i = 0; i < min; i++) { if (left.charAt(i) != right.charAt(i)) { return left.substring(0, i); } } return left.substring(0, min); } }