一个人的朝圣 — LeetCode打卡第 天
知识总结
今天重新学习了一下前缀树以及相关的知识
Leetcode 208. 实现 Trie (前缀树)
题目链接
题目说明
Trie(发音类似 “try”)或者说 前缀树 是一种树形数据结构,用于高效地存储和检索字符串数据集中的键。这一数据结构有相当多的应用情景,例如自动补完和拼写检查。
请你实现 Trie 类:
- Trie() 初始化前缀树对象。
- void insert(String word) 向前缀树中插入字符串 word 。
- boolean search(String word) 如果字符串 word 在前缀树中,返回 true(即,在检索之前已经插入);否则,返回 false 。
- boolean startsWith(String prefix) 如果之前已经插入的字符串 word 的前缀之一为 prefix ,返回 true ;否则,返回 false 。
代码说明
这是比较经典的, 应该很快写出来, 通过数组来实现树的结构
 写法: 先定义TrieNode, 然后再写插入和查询的方法.
class TrieNode{
    TrieNode[] children;
    boolean isEnd;
    public TrieNode(){
        children = new TrieNode[26];
        isEnd = false;
    }
 }
class Trie {
    private TrieNode root;
    public Trie() {
        root = new TrieNode();
    }
    
    public void insert(String word) {
        TrieNode cur = root;
        for(int i =0; i < word.length(); i++){
            char c = word.charAt(i);
            int index = c - 'a';
            if(cur.children[index] == null){
                cur.children[index] = new TrieNode();
            }
            cur = cur.children[index];
        }
        cur.isEnd = true;
    }
    
    public boolean search(String word) {
        TrieNode cur = root;
        for(int i =0; i < word.length(); i++){
            char c = word.charAt(i);
            int index = c - 'a';
            if(cur.children[index] == null){
                return false;
            }
            cur = cur.children[index];
        }
        return cur.isEnd;
    }
    
    public boolean startsWith(String prefix) {
        TrieNode cur = root;
        for(int i =0; i < prefix.length(); i++){
            char c = prefix.charAt(i);
            int index = c - 'a';
            if(cur.children[index] == null){
                return false;
            }
            cur = cur.children[index];
        }
        return true;
    }
}
/**
 * Your Trie object will be instantiated and called as such:
 * Trie obj = new Trie();
 * obj.insert(word);
 * boolean param_2 = obj.search(word);
 * boolean param_3 = obj.startsWith(prefix);
 */
Leetcode 211. 添加与搜索单词 - 数据结构设计
题目链接
题目说明
请你设计一个数据结构,支持 添加新单词 和 查找字符串是否与任何先前添加的字符串匹配 。
实现词典类 WordDictionary :
- WordDictionary() 初始化词典对象
- void addWord(word) 将 word 添加到数据结构中,之后可以对它进行匹配
- bool search(word) 如果数据结构中存在字符串与 word 匹配,则返回 true ;否则,返回 false 。word 中可能包含一些 ‘.’ ,每个 . 都可以表示任何一个字母。

代码说明
这一题再原有的基础上变化了, 在搜索的时候出现了 “.” 能够匹配任意项, 需要用到DFS算法.
DFS和回溯很像, 但是也有不同的, 就是不需要回退. 然后递推公式就为
if(child != null && dfs(child, index + 1, word)){ return true;}
学习新的方法
Character.isLetter(ch)
class TrieNode{
    TrieNode[] children;
    boolean isEnd;
    public TrieNode(){
        children = new TrieNode[26];
        isEnd = false;
    }
}
class WordDictionary {
    private TrieNode root;
    public WordDictionary() {
        root = new TrieNode();
    }
    
    public void addWord(String word) {
        TrieNode cur = root;
        for(int i =0; i < word.length(); i++){
            char c = word.charAt(i);
            int index = c - 'a';
            if(cur.children[index] == null){
                cur.children[index] = new TrieNode();
            }
            cur = cur.children[index];
        }
        cur.isEnd = true;
    }
    public boolean dfs(TrieNode node, int index, String word){
        if(index == word.length()){
            return node.isEnd;
        }
        char ch = word.charAt(index);
        if(Character.isLetter(ch)){
            int charIndex = ch - 'a';
            TrieNode child = node.children[charIndex];
            if(child != null && dfs(child, index + 1, word)){
                return true;
            }
        }else{
            // 遍历这一层
            for(int i = 0; i < 26; i++){
                TrieNode child = node.children[i];
                if(child!=null && dfs(child, index + 1, word)){
                    return true;
                }
            }
        }
        return false;
    }
    
    public boolean search(String word) {
        return dfs(root, 0, word);
    }
}
/**
 * Your WordDictionary object will be instantiated and called as such:
 * WordDictionary obj = new WordDictionary();
 * obj.addWord(word);
 * boolean param_2 = obj.search(word);
 */
Leetcode 648. 单词替换
题目链接
题目说明
在英语中,我们有一个叫做 词根(root) 的概念,可以词根后面添加其他一些词组成另一个较长的单词——我们称这个词为 继承词(successor)。例如,词根an,跟随着单词 other(其他),可以形成新的单词 another(另一个)。
现在,给定一个由许多词根组成的词典 dictionary 和一个用空格分隔单词形成的句子 sentence。你需要将句子中的所有继承词用词根替换掉。如果继承词有许多可以形成它的词根,则用最短的词根替换它。
你需要输出替换之后的句子。

代码说明
字节面试题
 用前缀树实现的话变动的其实只有查询的方法
class TrieNode{
    TrieNode[] children;
    boolean isEnd;
    public TrieNode(){
        children = new TrieNode[26];
        isEnd = false;
    }
}
class Solution {
    TrieNode root = new TrieNode();
    public void add(String s){
        TrieNode cur = root;
        for(int i = 0; i < s.length(); i++){
            char ch = s.charAt(i);
            int index = ch - 'a';
            if(cur.children[index] == null){
                cur.children[index] = new TrieNode();
            }
            cur = cur.children[index];
        }
        cur.isEnd = true;
    }
    public String query(String s){
        // 查找s对应的词根, 如果有就返回词根, 没有找到就返回自己
        TrieNode cur = root;
        for(int i = 0; i < s.length(); i++){
            int index = s.charAt(i) - 'a';
            if(cur.children[index] == null) break;
            if(cur.children[index].isEnd) return s.substring(0, i+1);
            cur = cur.children[index];
        }
        return s;
    }
    public String replaceWords(List<String> dictionary, String sentence) {
        for(String str : dictionary){
            add(str);
        } 
        StringBuilder sb = new StringBuilder();
        for(String word : sentence.split(" ")){
            System.out.println(query(word));
            sb.append(query(word)).append(" ");
        }
        return sb.substring(0, sb.length() -1);
    }
}


















