Given a list of strings of the same length and no special characters:
给定长度相同且没有特殊字符的字符串列表:
List<String> myStringArray = List.of( "xxxyyyjjjabcdefg", xxxyyyjjjabcdefg", "xxxyyyxxxabcdefg")
;
List<字符串>myStringArray=List.of(“xxxyyyjjabcDefg”,xxxyyyjjabcDefg“,”xxxyyyxxxabcDefg“);
I can find the longest common sub string of the list:
我可以找到列表中最长的公共子字符串:
String lengthOfAllStrings = myStringArray.stream().min(Comparator.comparingInt(String::length)).get();
List<char[]> myCharArray = myStringArray.stream().map(String::toCharArray).collect(Collectors.toList());
StringBuilder longestCommonSubString = StringBuilder();
StringBuilder currentLongestSubString = new StringBuilder();
for(int i = 0; i < lengthOfAllStrings.length(); i++) {
int finalI = i;
if(myCharArray.stream().anyMatch((a) -> a[finalI] == ( (myCharArray.stream().collect(Collectors.groupingBy(x -> x[finalI]))).values().size == 1 ? (myCharArray.stream().collect(Collectors.groupingBy(x -> x[finalI]))).entrySet().iterator().next().getKey() : '!'))) {
longestCommonSubString.append((myCharArray.stream().collect(Collectors.groupingBy( x -> x[finalI]))).entrySet().iterator().next().getKey());
} else {
currentLongestSubString.append(longestCommonSubString);
longestCommonSubString = new StringBuilder();
}
}
System.out.println("The Longest Common Sub String = " + Stream.of(longestCommonSubString.toString(), currentLongestSubString.toString()).max(Comparator.comparingInt(String::length)).get());
Output: The Longest Common Sub String = abcdefg
输出:最长公共子字符串=ABCDEFG
but what if the list contains strings of different length:
但是,如果列表包含不同长度的字符串怎么办:
List<String> myStringArray = List.of( "xxxyyy", xxxyyy", "yyy");
List<字符串>myStringArray=List.of(“xxxyyy”,xxxyyy“,”yyy“);
Desired Output: The Longest Sub String = yyy
所需输出:最长的子字符串=yyy
Also, is there a way to do all this with a one liner and not use a for loop ?
另外,有没有一种方法可以在不使用for循环的情况下使用one linder来完成所有这些任务?
更多回答
Do you really think that performing multi-step stream operations inside sub-expressions inside an if
condition has an acceptable readability? Not to speak of the efficiency of performing the same stream operation multiple times in a row. And why do you convert all strings into char[]
arrays at the beginning? As far as I can see, the only thing you’re doing with the arrays, is the equivalent of a simple charAt
call which you can do on the original strings.
您真的认为在IF条件内子表达式中执行多步流操作具有可接受的可读性吗?更不用说连续多次执行相同的流操作的效率了。为什么要在开头将所有字符串转换为char[]数组?据我所知,您对数组所做的唯一事情是,您可以对原始字符串执行一个简单的charat调用。
优秀答案推荐
You can try this:
您可以尝试以下操作:
import java.util.*;
public class LongestCommonSubstring {
public static String findLongestCommonSubstring(List<String> strings) {
if (strings == null || strings.isEmpty()) {
return "";
}
String firstString = strings.get(0);
int maxLength = firstString.length();
String longestCommonSubstring = "";
for (int i = 0; i < maxLength; i++) {
for (int j = i + 1; j <= maxLength; j++) {
String substring = firstString.substring(i, j);
boolean isCommon = strings.stream().allMatch(s -> s.contains(substring));
if (isCommon && substring.length() > longestCommonSubstring.length()) {
longestCommonSubstring = substring;
}
}
}
return longestCommonSubstring;
}
public static void main(String[] args) {
List<String> myStringArray = List.of("xxxyyy", "xxxyyy", "yyy");
String longestCommonSubstring = findLongestCommonSubstring(myStringArray);
System.out.println("The Longest Common Substring = " + longestCommonSubstring);
}
}
This code will work for lists of strings with different lengths and will find the longest common substring among them.
此代码将用于不同长度的字符串列表,并将找到其中最长的公共子字符串。
As for doing this with a one-liner and without a for loop, it's not practical because finding the longest common substring among different-length strings typically requires comparing all possible substrings, which cannot be efficiently accomplished with a one-liner or without some form of iteration. Hope this can help :)
至于使用一行程序而不使用for循环来执行此操作,这是不实际的,因为在不同长度的字符串中查找最长的公共子字符串通常需要比较所有可能的子字符串,而使用一行程序或不使用某种形式的迭代无法有效地实现这一点。希望这能有所帮助:)
更多回答
我是一名优秀的程序员,十分优秀!