Subject address:
https://www.lintcode.com/problem/concatenated-words/description
Given an array of non-empty English lowercase strings AAA. The title guarantees that the strings are different from each other, and returns all the strings that can be concatenated by at least two other strings. The same string can be used multiple times.
Refer to https://blog.csdn.net/qq_46105170/article/details/112428099 . code show as below:
import java.util.*;
public class Solution {
/**
* @param words: List[str]
* @return: return List[str]
*/
public List<String> findAllConcatenatedWordsInADict(String[] words) {
// write your code here
List<String> res = new ArrayList<>();
Set<String> set = new HashSet<>();
Arrays.sort(words, (w1, w2) -> Integer.compare(w1.length(), w2.length()));
for (int i = 0; i < words.length; i++) {
if (check(words[i], set)) {
res.add(words[i]);
}
set.add(words[i]);
}
return res;
}
private boolean check(String s, Set<String> set) {
int[] dp = new int[s.length() + 1];
Arrays.fill(dp, -1);
dp[0] = 0;
for (int i = 0; i < s.length(); i++) {
if (dp[i] == -1) {
continue;
}
for (int j = s.length() - i; j >= 0; j--) {
if (set.contains(s.substring(i, i + j))) {
dp[i + j] = Math.max(dp[i + j], dp[i] + 1);
}
if (dp[s.length()] > 1) {
return true;
}
}
}
return false;
}
}
Time complexity O (nl log n + nl 3) O(nl\log n+nl^3)O(nllogn+nl3), l l l is the longest string length, spaceO (nl) O(nl)O(nl)。