![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/4e10210defac2fc9dfd10aa953e21227.png)
class Solution {
public String longestWord(String[] words) {
Arrays.sort(words, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
if(o1.length() == o2.length()){
return o1.compareTo(o2);
}else{
return o2.length() - o1.length();
}
}
});
Set<String> set = new HashSet<>();
for (String s : words){
set.add(s);
}
for (String word : words){
set.remove(word);
if(find(set, word)){
return word;
}
}
return "";
}
private boolean find(Set<String> set, String word) {
if(word.length() == 0){
return true;
}
for (int i = 0; i < word.length(); i++) {
if(set.contains(word.substring(0, i + 1)) && find(set, word.substring(i + 1))) {
return true;
}
}
return false;
}
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/d60ad8b6f8cb2045e0e61bba8ff1ea6c.png)
public int respace(String[] dictionary, String sentence) {
Set<String> set = new HashSet<>();
for (String s : dictionary){
set.add(s);
}
int[] dp = new int[sentence.length() + 1];
char[] str = sentence.toCharArray();
for (int i = 1; i <= str.length; i++) {
dp[i] = dp[i - 1] + 1;
for (int j = 0; j < i; j++) {
if(set.contains(sentence.substring(j, i))){
dp[i] = Math.min(dp[i], dp[j]);
}
}
}
return dp[sentence.length()];
}
class Trie {
class TrieNode{
boolean isEnd;
TrieNode[] nexts;
public TrieNode(){
isEnd = false;
nexts = new TrieNode[26];
}
}
TrieNode root;
public Trie(){
root = new TrieNode();
}
public void insert(String word) {
char[] str = word.toCharArray();
TrieNode node = root;
for (int i = 0; i < word.length(); i++) {
int index = str[i] - 'a';
if(node.nexts[index] == null){
node.nexts[index] = new TrieNode();
}
node = node.nexts[index];
}
node.isEnd = true;
}
public boolean search(String word) {
char[] str = word.toCharArray();
TrieNode node = root;
for (int i = 0; i < str.length; i++) {
int index = str[i] - 'a';
if(node.nexts[index] == null){
return false;
}
node = node.nexts[index];
}
return node.isEnd;
}
public boolean startsWith(String prefix) {
char[] str = prefix.toCharArray();
TrieNode node = root;
for (int i = 0; i < str.length; i++) {
int index = str[i] - 'a';
if(node.nexts[index] == null){
return false;
}
node = node.nexts[index];
}
return true;
}
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/444ce93b0b9c3e77b7fdb78781afe82c.png)
public String replaceWords(List<String> dictionary, String sentence) {
Set<String> set = new HashSet<>();
for (int i = 0; i < dictionary.size(); i++) {
set.add(dictionary.get(i));
}
String[] strs = sentence.split(" ");
for (int i = 0; i < strs.length; i++) {
String s = strs[i];
for (int j = 0; j < s.length(); j++) {
if (set.contains(s.substring(0, j + 1))) {
strs[i] = s.substring(0, j + 1);
break;
}
}
}
String join = String.join(" ", strs);
return join;
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/109bf6520eaf92b68e3f8d6d0ae6d5c1.png)
public boolean wordBreak(String s, List<String> wordDict) {
boolean[] dp = new boolean[s.length() + 1];
Set<String> set = new HashSet<>(wordDict);
dp[0] = true;
for(int i = 1; i <= s.length(); i++){
for(int j = 0; j < i; j++){
if(set.contains(s.substring(j,i)) && dp[j]){
dp[i] = true;
break;
}
}
}
return dp[s.length()];
}