public static int commonPrefixSegments(char separator, Collection<String> strings, int minSize) { Preconditions.checkNotNull(strings, "strings is null"); Preconditions.checkArgument(minSize >= 0, "minSize is negative"); if (strings.isEmpty()) { return 0; } int shortestNumberOfParts = Integer.MAX_VALUE; List<List<String>> stringsParts = newArrayList(); for (String string : strings) { List<String> parts = ImmutableList.copyOf(Splitter.on(separator).split(string)); if (parts.isEmpty() || !parts.get(0).isEmpty()) { throw new IllegalArgumentException("All strings must start with the separator character"); } parts = parts.subList(1, parts.size()); stringsParts.add(parts); shortestNumberOfParts = min(parts.size(), shortestNumberOfParts); } int maxNumberOfSharedParts = max(shortestNumberOfParts - minSize, 0); int commonParts = 0; while (commonParts < maxNumberOfSharedParts && isPartEqual(commonParts, stringsParts)) { commonParts++; } return commonParts; }