【字符串】实现一个魔法字典-字典树

题目描述

设计一个使用单词列表进行初始化的数据结构,单词列表中的单词 互不相同 。 如果给出一个单词,请判定能否只将这个单词中一个字母换成另一个字母,使得所形成的新单词存在于你构建的字典中。

实现 MagicDictionary 类:

MagicDictionary() 初始化对象
void buildDict(String[] dictionary) 使用字符串数组 dictionary 设定该数据结构,dictionary 中的字符串互不相同
bool search(String searchWord) 给定一个字符串 searchWord ,判定能否只将字符串中 一个 字母换成另一个字母,使得所形成的新字符串能够与字典中的任一字符串匹配。如果可以,返回 true ;否则,返回 false 。
 

示例:

输入
["MagicDictionary", "buildDict", "search", "search", "search", "search"]
[[], [["hello", "leetcode"]], ["hello"], ["hhllo"], ["hell"], ["leetcoded"]]
输出
[null, null, false, true, false, false]

解释
MagicDictionary magicDictionary = new MagicDictionary();
magicDictionary.buildDict(["hello", "leetcode"]);
magicDictionary.search("hello"); // 返回 False
magicDictionary.search("hhllo"); // 将第二个 'h' 替换为 'e' 可以匹配 "hello" ,所以返回 True
magicDictionary.search("hell"); // 返回 False
magicDictionary.search("leetcoded"); // 返回 False

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/implement-magic-dictionary

解题思路

第一种思路,直接使用MAP,将所有case都计算一遍,这个方法最终耗时过高放弃。

第二种思路,在查询的时候遍历字符串,如果check条件是有一个字符不相同,则返回true,否则重试,如果都不满足条件,返回false;

第三种思路,对第二种进行调整,提前记录长度和字符串的关系,根据长度来获取字符串,然后按照第二种思路执行,耗时有所下降

第四种思路,使用字典树,记录每个字符串的字典树;然后使用字符串匹配字典树;匹配条件:

  • 如果已经到字符串结束位置,判断字典树已经结束,并且有被修改的记录。
  • 如果字典树中有匹配的字符,则递归调用,如果结果为true,则直接返回true,否则进入下一步
  • 如果字典树中没有匹配字符,则剔除当前字符所在位置后,找到一个新的存在的字符,做递归调用,并且将修改标记设置为true;

代码实现

第一种实现,直接使用MAP,将所有case都计算一遍


import java.util.HashMap;
import java.util.Map;

class MagicDictionary1 {

    Map<Integer, Map<String, String>> map;

    public MagicDictionary1() {
        this.map = new HashMap<>();
    }

    public void buildDict(String[] dictionary) {

        for (String s : dictionary) {
            Map<String, String> newMap = map.get(s.length());
            if (newMap == null) {
                map.put(s.length(), new HashMap<>());
                newMap = map.get(s.length());
            }

            for (int i = 0; i < s.length(); i++) {

                for (char c = 'a'; c <= 'z'; c++) {
                    if(s.charAt(i) == c) {
                        continue;
                    }
                    if (i == 0) {
                        newMap.put(c + s.substring(1), s);
                    } else if (i == s.length() - 1) {
                        newMap.put(s.substring(0, s.length() - 1) + c, s);
                    } else {
                        String s1 = s.substring(0, i) + c + s.substring(i + 1);
                        newMap.put(s1, s);
                    }
                }
            }
        }

    }

    public boolean search(String searchWord) {
        int length = searchWord.length();
        Map<String, String> newMap = map.get(length);
        if (newMap != null) {
            return newMap.get(searchWord) != null;
        }
        return false;
    }

第二种实现:


class MagicDictionary2 {

    String[] dictionary;

    public MagicDictionary2() {

    }

    public void buildDict(String[] dictionary) {
        this.dictionary = dictionary;
    }

    public boolean search(String searchWord) {
        for (String d : dictionary) {
            int length = searchWord.length();
            if (d.length() != length) {
                continue;
            }

            if (isOneDif(d, searchWord)) {
                return true;
            }
        }
        return false;
    }

    private boolean isOneDif(String d, String searchWord) {

        int dif = 0;
        for (int i = 0; i < d.length(); i++) {
            if (d.charAt(i) == searchWord.charAt(i)) {
                continue;
            } else {
                dif++;
            }
        }
        return dif == 1;
    }

    //["MagicDictionary", "buildDict", "search", "search", "search", "search"]
    //[[], [["hello","hallo","leetcode"]], ["hello"], ["hallo"], ["hell"], ["leetcodd"]]
    public static void main(String[] args) {
        MagicDictionary2 dictionary = new MagicDictionary2();
        dictionary.buildDict(new String[]{"hello", "hallo", "leetcode"});

        System.out.println(dictionary.search("leetcodd"));
    }
}

第三种实现:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

class MagicDictionary3 {

    Map<Integer, List<String>> map;

    public MagicDictionary3() {
        map = new HashMap<>();
    }

    public void buildDict(String[] dictionary) {
        for (String d : dictionary) {
            int length = d.length();
            List<String> list = map.get(length);
            if (list == null) {
                list = new ArrayList<>();
                map.put(length, list);
            }
            list.add(d);
        }
    }

    public boolean search(String searchWord) {
        List<String> dictionary = map.get(searchWord.length());
        if (dictionary == null) {
            return false;
        }
        for (String d : dictionary) {
            if (isOneDif(d, searchWord)) {
                return true;
            }
        }
        return false;
    }

    private boolean isOneDif(String d, String searchWord) {

        int dif = 0;
        for (int i = 0; i < d.length(); i++) {
            if (d.charAt(i) == searchWord.charAt(i)) {
                continue;
            } else {
                dif++;
            }
        }
        return dif == 1;
    }

    //["MagicDictionary", "buildDict", "search", "search", "search", "search"]
    //[[], [["hello","hallo","leetcode"]], ["hello"], ["hallo"], ["hell"], ["leetcodd"]]
    public static void main(String[] args) {
        MagicDictionary3 dictionary = new MagicDictionary3();
        dictionary.buildDict(new String[]{"hello", "hallo", "leetcode"});

        System.out.println(dictionary.search("leetcodd"));
    }
}

第四种实现,字典树:



class MagicDictionary {

    Trie root;

    public MagicDictionary() {
        this.root = new Trie();
    }

    public void buildDict(String[] dictionary) {

        for (String dic : dictionary) {
            insert(dic, root);
        }

    }

    private void insert(String dic, Trie root) {

        for (int i = 0; i < dic.length(); i++) {
            char c = dic.charAt(i);
            int index = c - 'a';

            Trie newTrie = root.children[index];
            if (newTrie == null) {
                newTrie = new Trie();
                root.children[index] = newTrie;
            }
            if (i >= dic.length() - 1) {
                newTrie.isFinished = true;
            }
            root = newTrie;
        }

    }

    public boolean search(String searchWord) {

        return dfs(searchWord, root, 0, false);
    }

    private boolean dfs(String searchWord, Trie root, int position, boolean modified) {

        if (position >= searchWord.length()) {
            return root.isFinished && modified;
        }

        char c = searchWord.charAt(position);
        int index = c - 'a';

        Trie trie = root.children[index];

        if (trie != null) {
            if (dfs(searchWord, trie, position + 1, modified)) {
                return true;
            }
        }

        if (!modified) {
            for (int i = 0; i < 26; i++) {
                if (i != index && root.children[i] != null) {
                    if (dfs(searchWord, root.children[i], position + 1, true)) {
                        return true;
                    }
                }
            }
            return false;
        }
        return false;

    }

    static class Trie {
        boolean isFinished;
        Trie[] children;

        public Trie() {
            this.isFinished = false;
            this.children = new Trie[26];
        }

    }


    //["MagicDictionary", "buildDict", "search", "search", "search", "search"]
    //[[], [["hello","hallo","leetcode"]], ["hello"], ["hallo"], ["hell"], ["leetcodd"]]
    public static void main(String[] args) {
        MagicDictionary dictionary = new MagicDictionary();
        dictionary.buildDict(new String[]{"hello", "hallo", "leetcode"});

        System.out.println(dictionary.search("leetcodd"));
        System.out.println(dictionary.search("hello"));
    }

总结

第一种实现是完全暴力解法,耗时很高,没有通过;

第二种也是常见思路,提上去耗时在30ms;

第三种思路度第二种进行改进,提上去耗时在28ms;

第四种思路比较巧妙,之前没有学过字典树,先学习后,再做实现,过程中也看过其他人的解题代码,最终耗时25ms;

从效果上看第四种时间复杂度更低,理论上还有优化空间,欢迎评论。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值