返回介绍

lcof2 / 剑指 Offer II 064. 神奇的字典 / README

发布于 2024-06-17 01:04:41 字数 6851 浏览 0 评论 0 收藏 0

剑指 Offer II 064. 神奇的字典

题目描述

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

实现 MagicDictionary 类:

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

 

示例:

输入
inputs = ["MagicDictionary", "buildDict", "search", "search", "search", "search"]
inputs = [[], [["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

 

提示:

  • 1 <= dictionary.length <= 100
  • 1 <= dictionary[i].length <= 100
  • dictionary[i] 仅由小写英文字母组成
  • dictionary 中的所有字符串 互不相同
  • 1 <= searchWord.length <= 100
  • searchWord 仅由小写英文字母组成
  • buildDict 仅在 search 之前调用一次
  • 最多调用 100search

 

注意:本题与主站 676 题相同: https://leetcode.cn/problems/implement-magic-dictionary/

解法

方法一

class MagicDictionary:
  def __init__(self):
    """
    Initialize your data structure here.
    """

  def _patterns(self, word):
    return [word[:i] + '*' + word[i + 1 :] for i in range(len(word))]

  def buildDict(self, dictionary: List[str]) -> None:
    self.words = set(dictionary)
    self.counter = Counter(p for word in dictionary for p in self._patterns(word))

  def search(self, searchWord: str) -> bool:
    for p in self._patterns(searchWord):
      if self.counter[p] > 1 or (
        self.counter[p] == 1 and searchWord not in self.words
      ):
        return True
    return False


# Your MagicDictionary object will be instantiated and called as such:
# obj = MagicDictionary()
# obj.buildDict(dictionary)
# param_2 = obj.search(searchWord)
class MagicDictionary {
  private Set<String> words;
  private Map<String, Integer> counter;

  /** Initialize your data structure here. */
  public MagicDictionary() {
    words = new HashSet<>();
    counter = new HashMap<>();
  }

  public void buildDict(String[] dictionary) {
    for (String word : dictionary) {
      words.add(word);
      for (String p : patterns(word)) {
        counter.put(p, counter.getOrDefault(p, 0) + 1);
      }
    }
  }

  public boolean search(String searchWord) {
    for (String p : patterns(searchWord)) {
      int cnt = counter.getOrDefault(p, 0);
      if (cnt > 1 || (cnt == 1 && !words.contains(searchWord))) {
        return true;
      }
    }
    return false;
  }

  private List<String> patterns(String word) {
    List<String> res = new ArrayList<>();
    char[] chars = word.toCharArray();
    for (int i = 0; i < chars.length; ++i) {
      char c = chars[i];
      chars[i] = '*';
      res.add(new String(chars));
      chars[i] = c;
    }
    return res;
  }
}

/**
 * Your MagicDictionary object will be instantiated and called as such:
 * MagicDictionary obj = new MagicDictionary();
 * obj.buildDict(dictionary);
 * boolean param_2 = obj.search(searchWord);
 */
class MagicDictionary {
public:
  /** Initialize your data structure here. */
  MagicDictionary() {
  }

  void buildDict(vector<string> dictionary) {
    for (string word : dictionary) {
      words.insert(word);
      for (string p : patterns(word)) ++counter[p];
    }
  }

  bool search(string searchWord) {
    for (string p : patterns(searchWord)) {
      if (counter[p] > 1 || (counter[p] == 1 && !words.count(searchWord))) return true;
    }
    return false;
  }

private:
  unordered_set<string> words;
  unordered_map<string, int> counter;

  vector<string> patterns(string word) {
    vector<string> res;
    for (int i = 0; i < word.size(); ++i) {
      char c = word[i];
      word[i] = '*';
      res.push_back(word);
      word[i] = c;
    }
    return res;
  }
};

/**
 * Your MagicDictionary object will be instantiated and called as such:
 * MagicDictionary* obj = new MagicDictionary();
 * obj->buildDict(dictionary);
 * bool param_2 = obj->search(searchWord);
 */
type MagicDictionary struct {
  words   map[string]bool
  counter map[string]int
}

/** Initialize your data structure here. */
func Constructor() MagicDictionary {
  return MagicDictionary{
    words:   make(map[string]bool),
    counter: make(map[string]int),
  }
}

func (this *MagicDictionary) BuildDict(dictionary []string) {
  for _, word := range dictionary {
    this.words[word] = true
    for _, p := range patterns(word) {
      this.counter[p]++
    }
  }
}

func (this *MagicDictionary) Search(searchWord string) bool {
  for _, p := range patterns(searchWord) {
    if this.counter[p] > 1 || (this.counter[p] == 1 && !this.words[searchWord]) {
      return true
    }
  }
  return false
}

func patterns(word string) []string {
  var res []string
  for i := 0; i < len(word); i++ {
    res = append(res, word[:i]+"."+word[i+1:])
  }
  return res
}

/**
 * Your MagicDictionary object will be instantiated and called as such:
 * obj := Constructor();
 * obj.BuildDict(dictionary);
 * param_2 := obj.Search(searchWord);
 */

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文