Leetcode676 实现魔法字典

 

  字典树实现,JAVA:

class MagicDictionary {
        Node root;

        /**
         * Initialize your data structure here.
         */
        public MagicDictionary() {
        }

        public void buildDict(String[] dictionary) {
            this.root = new Node();
            for (String s : dictionary) this.insert(this.root, s);
        }

        public final boolean search(String searchWord) {
            return search(this.root, searchWord, 0, 0);
        }

        public final boolean search(Node node, String searchWord, int errorNum, int point) {
            if (node == null || errorNum == 2) return false;
            if (point == searchWord.length()) return errorNum == 1 && node.isEnd;
            int cPoint = searchWord.charAt(point) - 'a';
            boolean re = false;
            for (int i = 0; i < 26; i++) {
                if (i == cPoint && node.childs[cPoint] != null)
                    re |= search(node.childs[i], searchWord, errorNum, point + 1);
                else re |= search(node.childs[i], searchWord, errorNum + 1, point + 1);
            }
            return re;
        }

        private void insert(Node root, String str) {
            Node node = root, pre = root;
            for (int i = 0; i < str.length(); i++) {
                char c = str.charAt(i);
                int cPoint = c - 'a';
                if (node.childs[cPoint] == null) node.childs[cPoint] = new Node();
                node = node.childs[cPoint];
            }
            node.isEnd = true;
        }

        private class Node {
            Node[] childs;
            boolean isEnd;
            boolean preEnd;

            Node() {
                this.childs = new Node[26];
                this.preEnd = this.isEnd = false;
            }
        }
    }

  字典树实现,JS:

/**
 * Initialize your data structure here.
 */
var MagicDictionary = function () {
    this.root = new Node();
};

/**
 * @param {string[]} dictionary
 * @return {void}
 */
MagicDictionary.prototype.buildDict = function (dictionary) {
    for (let i = 0; i < dictionary.length; i++) insert(this.root, dictionary[i]);
};

/**
 * @param {string} searchWord
 * @return {boolean}
 */
MagicDictionary.prototype.search = function (searchWord) {
    return searchHandle(this.root, searchWord, 0, 0);
};

var searchHandle = function (node, str, strPoint, errorNum) {
    if (errorNum == 2 || !node) return false;
    if (strPoint == str.length) return node.isEnd && errorNum == 1;
    let cPoint = str.charAt(strPoint).charCodeAt() - 97, re = false;
    for (let i = 0; i < 26; i++) {
        if (i == cPoint && node.childs[cPoint]) re |= searchHandle(node.childs[i], str, strPoint + 1, errorNum);
        else re |= searchHandle(node.childs[i], str, strPoint + 1, errorNum + 1);
    }
    return re;
}

var insert = function (root, str) {
    let node = root;
    for (let i = 0; i < str.length; i++) {
        let cPiont = str.charAt(i).charCodeAt() - 97;
        if (!node.childs[cPiont]) node.childs[cPiont] = new Node();
        node = node.childs[cPiont];
    }
    node.isEnd = true;
}

var Node = function () {
    this.isEnd = false;
    this.childs = new Array(26);
}

 

posted @ 2021-02-08 15:14  牛有肉  阅读(55)  评论(0编辑  收藏  举报