LeetCode 676(C#)

题目

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

实现 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

代码

  • 前缀树
public class MagicDictionary 
{
    private Trie _trie;

    public MagicDictionary() 
    {
        _trie = new Trie();
    }
    
    public void BuildDict(string[] dictionary) => Array.ForEach(dictionary, s => _trie.Add(s));

    public bool Search(string searchWord) => _trie.Query(searchWord);

    //树节点
    private class TrieNode
    {
        public bool IsEnd { get; set; }
        public TrieNode[] Child { get; set; }

        public TrieNode()
        {
            IsEnd = false;
            Child = new TrieNode[26];
        }
    }
    //树
    private class Trie
    {
        private TrieNode _root;

        public Trie()
        {
            _root = new TrieNode();
        }

        public void Add(string s)
        {
            TrieNode node = _root;
            for (int i = 0; i < s.Length; i++)
            {
                int u = s[i] - 'a';
                if (node.Child[u] == null)
                    node.Child[u] = new TrieNode();
                node = node.Child[u];
            }

            node.IsEnd = true;
        }

        public bool Query(string s) => Query(s, _root, 0, false);

        private bool Query(string s, TrieNode node, int idx, bool replace)
        {
            if (idx == s.Length)
                return  replace && node.IsEnd;
            
            int u = s[idx] - 'a';
            if (node.Child[u] != null)
            {
                if (Query(s, node.Child[u], idx + 1, replace))
                    return true;
            }

            if (!replace)
            {
                for (int i = 0; i < 26; i++)
                {
                    if (i != u && node.Child[i] != null)
                    {
                        if (Query(s, node.Child[i], idx + 1, !replace))
                            return true;
                    }
                }
            }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值