List of utility methods to do Array Common Prefix
String | longestCommonPrefix(String[] strs) longest Common Prefix if (strs == null || strs.length == 0) return ""; int minLen = Integer.MAX_VALUE; for (String str : strs) minLen = (minLen > str.length()) ? str.length() : minLen; for (int i = 0; i < minLen; i++) { char ch = strs[0].charAt(i); for (int j = 1; j < strs.length; j++) { ... |
String | longestCommonPrefix(String[] strs) longest Common Prefix if (strs == null || strs.length == 0) { return ""; if (strs.length == 1) { return strs[0]; StringBuilder prefix = new StringBuilder(); int idx = 0; ... |
String | longestCommonPrefix(String[] strs) longest Common Prefix if (null == strs || 0 == strs.length) return ""; String prefix = strs[0]; for (int i = 0; i < strs.length; i++) { String str = strs[i]; int j = 0; for (j = 0; j < prefix.length(); j++) { if (j >= str.length() || prefix.charAt(j) != str.charAt(j)) ... |
String | longestCommonPrefix(String[] strs) Simple approach to compare first, second, ... if (strs.length == 0) return ""; if (strs.length == 1) return strs[0]; int len = strs[0].length(); int i = 0; for (; i < len; i++) { for (int j = 1; j < strs.length; j++) { ... |
String | longestCommonPrefix(String[] strs) longest Common Prefix String str = ""; if (strs.length == 0) return str; if (strs.length == 1) return strs[0]; for (int i = 1; i < strs.length; i++) { if (i == 1) { int n = strs[0].length() > strs[1].length() ? strs[1].length() : strs[0].length(); ... |
String | longestCommonPrefix(String[] strs) longest Common Prefix if (strs.length == 0) return ""; int i = 0; String prefix = ""; String result = ""; boolean broken = false; while (true) { i++; ... |
String | longestCommonPrefix(String[] strs, int l, int r) longest Common Prefix if (l == r) { return strs[l]; } else { int mid = (l + r) / 2; String lcpLeft = longestCommonPrefix(strs, l, mid); String rcpRight = longestCommonPrefix(strs, mid + 1, r); return commonPrefix(lcpLeft, rcpRight); |
String | longestCommonPrefix1(String[] strs) Horizontal scanning if (strs.length == 0) { return ""; String prefix = strs[0]; for (int i = 0; i < strs.length; i++) { while (strs[i].indexOf(prefix) != 0) { prefix = prefix.substring(0, prefix.length() - 1); if (prefix.isEmpty()) { ... |
String | longestCommonPrefix2(String[] strs) Vertical scanning if (strs == null || strs.length == 0) { return ""; for (int i = 0; i < strs[0].length(); i++) { char c = strs[0].charAt(i); for (int j = 1; j < strs.length; j++) { if (i == strs[j].length() || c != strs[j].charAt(i)) { return strs[0].substring(0, i); ... |
String | longestCommonPrefix3(String[] strs) Divide and conquer if (strs == null || strs.length == 0) { return ""; return longestCommonPrefix(strs, 0, strs.length - 1); |