强化二 字典树 Trie

Trie 的考点

  • 实现一个 Trie
  • 比较 Trie 和 Hash 的优劣
  • 字符矩阵类问题使用 Trie 比 Hash 更高效
  • hash和trie查找一个单词在不在都是O(L) 但是由于trie用到L次寻址操作 所以比hash慢

Hash vs Trie

  • 互相可替代
  • Trie 耗费更少的空间 单次查询 Trie 耗费更多的时间 (复杂度相同,Trie 系数大一些)

注意:

  • 不要忘记初始化root

思路:
其实就是实现两个操作

  • 插入一个单词
  • 查找某个单词或前缀是否存在

208 Implement Trie (Prefix Tree)
211 Add and Search Word - Data structure design
*425 Word Squares 从给定字典中 找出能组成对称矩阵的所有组合
*212 Word Search II 在给定字符矩阵中 找所有字典中的词

208 Implement Trie (Prefix Tree)

class Trie {
    class TrieNode{
        TrieNode[] children;
        boolean isWord;
        TrieNode(){
            children = new TrieNode[26];
            isWord = false;
        }
    }

    TrieNode root;
    /** Initialize your data structure here. */
    public Trie() {
        root = new TrieNode();
    }
    
    /** Inserts a word into the trie. */
    public void insert(String word) {
        TrieNode current = root;
        for(char c : word.toCharArray()){
            int index = c - 'a';
            if(current.children[index]==null){
                current.children[index] = new TrieNode();
            }
            current = current.children[index];
        }
        current.isWord = true;
    }
    
    /** Returns if the word is in the trie. */
    public boolean search(String word) {
        TrieNode current = root;
        for(char c : word.toCharArray()){
            int index = c - 'a';
            if(current.children[index]==null){
                return false;
            }
            current = current.children[index];
        }
        return current.isWord;
    }
    
    /** Returns if there is any word in the trie that starts with the given prefix. */
    public boolean startsWith(String word) {
        TrieNode current = root;
        for(char c : word.toCharArray()){
            int index = c - 'a';
            if(current.children[index]==null){
                return false;
            }
            current = current.children[index];
        }
        return true;
    }
}

211 Add and Search Word - Data structure design

class WordDictionary {
    class Node{
        Node[] children;
        boolean isWord;
        Node(){
            children = new Node[26];
            isWord = false;
        }
    }
    Node root;
    /** Initialize your data structure here. */
    public WordDictionary() {
        root = new Node();
    }
    
    /** Adds a word into the data structure. */
    public void addWord(String word) {
        Node current = root;
        for(char c : word.toCharArray()){
            int index = c - 'a';
            if(current.children[index]==null){
                current.children[index] = new Node();
            }
            current = current.children[index];
        }
        current.isWord = true;
    }
    
    /** Returns if the word is in the data structure. A word could contain the dot character '.' to represent any one letter. */
    public boolean search(String word) {
        return find(word, 0, root);
    }
    
    private boolean find(String word, int index, Node node){
        if(index == word.length())
            return node.isWord;
        char c = word.charAt(index);
        if(c=='.'){
            for(int j=0; j<26; j++){
                    if(node.children[j]!=null){
                        if(find(word, index+1, node.children[j]))
                            return true;
                    }
                }
                return false;
        }else{
            return node.children[c-'a']!=null && find(word, index+1, node.children[c-'a']);
        }
    }
}

/**
 * Your WordDictionary object will be instantiated and called as such:
 * WordDictionary obj = new WordDictionary();
 * obj.addWord(word);
 * boolean param_2 = obj.search(word);
 */

425 Word Squares 从给定字典中 找出能组成对称矩阵的所有组合

class Solution {
    static class TrieNode{
        TrieNode[] children;
        boolean isWord;
        Set list;
        TrieNode(){
            children = new TrieNode[26];
            isWord = false;
            list = new HashSet<>();
        }
    }
    static TrieNode root;
    private static void build(String[] words){
        for(String s : words){
            buildHelper(s);
        }
    }
    private static void buildHelper(String s){
        TrieNode current = root;
        for(int i=0; i getWordsWithPrefix(String prefix){
        Set result = new HashSet<>();
        TrieNode current = root;
        for(int i=0; i> wordSquares(String[] words) {
        List> results = new ArrayList<>();
        if(words==null || words.length==0 || words[0].length()==0)
            return results;
        root = new TrieNode();
        build(words);
        helper(words, results, new ArrayList());
        return results;
    }
    
    private static void helper(String[] words, List> results, List subset){
        int size = words[0].length();
        if(subset.size() == size){
            results.add(new ArrayList(subset));
            return;
        }
        StringBuilder sb = new StringBuilder();
        for(String s : subset){
            sb.append(s.charAt(subset.size()));
        }
        String prefix = sb.toString();
        Set set = getWordsWithPrefix(prefix);
        for(String s : set){
            subset.add(s);
            helper(words, results, subset);
            subset.remove(subset.size()-1);
        }
    } 
}

212 Word Search II 在给定字符矩阵中 找所有字典中的词

  • 用hashmap
class Solution {
    public List findWords(char[][] board, String[] words) {
        Set prefixs = new HashSet<>();
        Set wordSet = new HashSet<>();
        for(int j=0; j results = new HashSet<>();
        for(int i=0; i(results);
    }
    private void helper(char[][] board, boolean[][] visited, int x, int y, Set prefixs, Set wordSet, Set results, String temp){ 
        if(wordSet.contains(temp)){
            results.add(temp);
        }
        if(!prefixs.contains(temp))
            return;
        
        int[] dirx = {1,-1,0,0};
        int[] diry = {0,0,1,-1};
        for(int i=0; i<4; i++){
            if(isValid(board, x+dirx[i], y+diry[i], visited)){
                visited[x+dirx[i]][y+diry[i]] = true;
                helper(board, visited, x+dirx[i], y+diry[i], prefixs, wordSet, results, temp+board[x+dirx[i]][y+diry[i]]);
                visited[x+dirx[i]][y+diry[i]] = false;
            }
        }   
    }
    private boolean isValid(char[][] board,int x, int y, boolean[][] visited){
        if(x>=0 && x=0 && y
  • 用trie
class Solution {
    static class TrieNode{
        TrieNode[] children;
        TrieNode(){
            children = new TrieNode[26];
        }
    }
    static TrieNode root;
    private static void build(String[] words){
        for(String word: words){
            builderHelper(word);
        }
    }
    private static void builderHelper(String word){
        TrieNode current = root;
        for(char c : word.toCharArray()){
            int index = c - 'a';
            if(current.children[index]==null)
                current.children[index] = new TrieNode();
            current = current.children[index];
        }
    }

    private static boolean startWith(String word){
        TrieNode current = root;
        for(char c : word.toCharArray()){
            int index = c - 'a';
            if(current==null || current.children[index]==null)
                return false;
            current = current.children[index];
        }
        return true;
    }

    public static List findWords(char[][] board, String[] words) {
        Set results = new HashSet<>();
        root = new TrieNode();
        build(words);
        Set set = new HashSet<>();
        for(String s: words){
            set.add(s);
        }
        boolean[][] visited = new boolean[board.length][board[0].length];
        for(int i=0; i solutions = new ArrayList<>();
        for(String s: results){
            solutions.add(s);
        }
        return solutions;
    }
    private static void helper(int row, int col, char[][] board, Set set, Set results, StringBuilder sb, boolean[][] visited){

        if(set.contains(sb.toString()))
            results.add(sb.toString());
        int[] dirx = {1, -1, 0, 0};
        int[] diry = {0, 0, -1, 1};
        for(int i=0; i<4; i++){
            int x = row + dirx[i];
            int y = col + diry[i];
            if(!valid(x, y, visited)){
                continue;
            }
            sb.append(board[x][y]);
            if(!startWith(sb.toString())){
                sb.deleteCharAt(sb.length()-1);
                continue;
            }
            visited[x][y] = true;
            helper(x, y, board, set, results, sb, visited);
            sb.deleteCharAt(sb.length()-1);
            visited[x][y] = false;
        }
    }
    private static boolean valid(int x, int y, boolean[][] visited){
        return x>=0 && x=0 && y

你可能感兴趣的:(强化二 字典树 Trie)