https://leetcode.com/problems/replace-words/
X. Trie
http://blog.csdn.net/u014688145/article/details/75911369
class TrieNode{ TrieNode[] children; String str; public TrieNode(){ children = new TrieNode[26]; } } public TrieNode add(TrieNode root, String str){ if (root == null) root = new TrieNode(); TrieNode cur = root; for (char c : str.toCharArray()){ int pos = c - 'a'; if (cur.children[pos] == null) cur.children[pos] = new TrieNode(); cur = cur.children[pos]; } cur.str = str; return root; } public String search(TrieNode root, String prefix){ if (root == null) return null; TrieNode cur = root; for (char c : prefix.toCharArray()){ int pos = c -'a'; if (cur.children[pos] == null){ break; } if (cur.children[pos] != null){ cur = cur.children[pos]; if (cur.str != null){ return cur.str; } } } return null; } TrieNode root; public String replaceWords(List<String> dict, String sentence) { root = new TrieNode(); for (String word : dict){ root = add(root, word); } String[] replace = sentence.split(" "); StringBuilder sb = new StringBuilder(); for (int i = 0; i < replace.length; ++i){ String key = search(root, replace[i]); if (key == null){ sb.append(replace[i] + " "); } else{ sb.append(key + " "); } } return sb.toString().substring(0, sb.length() - 1); }
https://discuss.leetcode.com/topic/96809/simple-java-8-and-trie-based-solution
X. Brute Force
https://leetcode.com/articles/replace-words/
【每日一题:小Fu讲解】LeetCode 648. Replace Words
In English, we have a concept called
root
, which can be followed by some other words to form another longer word - let's call this word successor
. For example, the root an
, followed by other
, which can form another word another
.
Now, given a dictionary consisting of many roots and a sentence. You need to replace all the
successor
in the sentence with the root
forming it. If a successor
has many roots
can form it, replace it with the root with the shortest length.
You need to output the sentence after the replacement.
Example 1:
Input: dict = ["cat", "bat", "rat"] sentence = "the cattle was rattled by the battery" Output: "the cat was rat by the bat"
Note:
- The input will only have lower-case letters.
- 1 <= dict words number <= 1000
- 1 <= sentence words number <= 1000
- 1 <= root length <= 100
- 1 <= sentence words length <= 1000
X. Trie
http://blog.csdn.net/u014688145/article/details/75911369
class TrieNode{ TrieNode[] children; String str; public TrieNode(){ children = new TrieNode[26]; } } public TrieNode add(TrieNode root, String str){ if (root == null) root = new TrieNode(); TrieNode cur = root; for (char c : str.toCharArray()){ int pos = c - 'a'; if (cur.children[pos] == null) cur.children[pos] = new TrieNode(); cur = cur.children[pos]; } cur.str = str; return root; } public String search(TrieNode root, String prefix){ if (root == null) return null; TrieNode cur = root; for (char c : prefix.toCharArray()){ int pos = c -'a'; if (cur.children[pos] == null){ break; } if (cur.children[pos] != null){ cur = cur.children[pos]; if (cur.str != null){ return cur.str; } } } return null; } TrieNode root; public String replaceWords(List<String> dict, String sentence) { root = new TrieNode(); for (String word : dict){ root = add(root, word); } String[] replace = sentence.split(" "); StringBuilder sb = new StringBuilder(); for (int i = 0; i < replace.length; ++i){ String key = search(root, replace[i]); if (key == null){ sb.append(replace[i] + " "); } else{ sb.append(key + " "); } } return sb.toString().substring(0, sb.length() - 1); }
https://discuss.leetcode.com/topic/96809/simple-java-8-and-trie-based-solution
The only modification to the standard Trie, is that we need a function getShortestPrefix that returns the shortest prefix of the given word in the trie, if the shortest prefix exists or return the original word. Once we have this, all we have to do is iterate through the sentence and replace each word with the getShortestPrefix(word) in the trie.
public String replaceWords(List<String> dict, String sentence) {
Trie trie = new Trie(256);
dict.forEach(s -> trie.insert(s));
List<String> res = new ArrayList<>();
Arrays.stream(sentence.split(" ")).forEach(str -> res.add(trie.getShortestPrefix(str)));
return res.stream().collect(Collectors.joining(" "));
}
class Trie {
private int R;
private TrieNode root;
public Trie(int R) {
this.R = R;
root = new TrieNode();
}
// Returns the shortest prefix of the word that is there in the trie
// If no such prefix exists, return the original word
public String getShortestPrefix(String word) {
int len = getShortestPrefix(root, word, -1);
return (len < 1) ? word : word.substring(0, len);
}
private int getShortestPrefix(TrieNode root, String word, int res) {
if(root == null || word.isEmpty()) return 0;
if(root.isWord) return res + 1;
return getShortestPrefix(root.next[word.charAt(0)], word.substring(1), res+1);
}
// Inserts a word into the trie.
public void insert(String word) {
insert(root, word);
}
private void insert(TrieNode root, String word) {
if(word.isEmpty()) {
root.isWord = true;
return;
}
if(root.next[word.charAt(0)] == null) {
root.next[word.charAt(0)] = new TrieNode();
}
insert(root.next[word.charAt(0)], word.substring(1));
}
private class TrieNode {
private TrieNode[] next = new TrieNode[R];
private boolean isWord;
}
}
https://leetcode.com/articles/replace-words/
- Time Complexity: where is the length of the -th word. We might check every prefix, the -th of which is work.
- Space Complexity: where is the length of our sentence; the space used by
rootset
.
public String replaceWords(List<String> dict, String sentence) {
if (dict == null || dict.size() == 0) return sentence;
Set<String> set = new HashSet<>();
for (String s : dict) set.add(s);
StringBuilder sb = new StringBuilder();
String[] words = sentence.split("\\s+");
for (String word : words) {
String prefix = "";
for (int i = 1; i <= word.length(); i++) {
prefix = word.substring(0, i);
if (set.contains(prefix)) break;
}
sb.append(" " + prefix);
}
return sb.deleteCharAt(0).toString();
}
X. Videos【每日一题:小Fu讲解】LeetCode 648. Replace Words