LC 676. Implement Magic Dictionary

Implement a magic directory with buildDict, and search methods.

For the method buildDict, you‘ll be given a list of non-repetitive words to build a dictionary.

For the method search, you‘ll be given a word, and judge whether if you modify exactly one character into another character in this word, the modified word is in the dictionary you just built.

Example 1:

Input: buildDict(["hello", "leetcode"]), Output: Null
Input: search("hello"), Output: False
Input: search("hhllo"), Output: True
Input: search("hell"), Output: False
Input: search("leetcoded"), Output: False

Note:

  1. You may assume that all the inputs are consist of lowercase letters a-z.
  2. For contest purpose, the test data is rather small by now. You could think about highly efficient algorithm after the contest.
  3. Please remember to RESET your class variables declared in class MagicDictionary, as static/class variables are persisted across multiple test cases. Please see here for more details.

Runtime: 0 ms, faster than 100.00% of C++ online submissions for Implement Magic Dictionary.

class MagicDictionary {
public:
  vector<string> strvec;
  explicit MagicDictionary() = default;

  void buildDict(const vector<string>& dict) {
    strvec = dict;
  }

  bool search(const string& word) {

    for(auto& vs : strvec){
      if(vs.size() != word.size()) continue;
      int idx = -1;
      bool fit = true;
      unordered_set<char> s;
      for(int i=0; i<word.size(); i++){
        s.insert(word[i]);
        if(idx == -1 && word[i] != vs[i]) {idx = i;}
        else if(idx != -1 && word[i] != vs[i]) {fit = false; break;}
      }
      if(idx != -1 && fit && s.count(word[idx])) return true;
    }
    return false;
  }
};

原文地址:https://www.cnblogs.com/ethanhong/p/10208027.html

时间: 2024-10-30 06:47:06

LC 676. Implement Magic Dictionary的相关文章

[LeetCode] 676. Implement Magic Dictionary 实现神奇字典

Implement a magic directory with buildDict, and search methods. For the method buildDict, you'll be given a list of non-repetitive words to build a dictionary. For the method search, you'll be given a word, and judge whether if you modify exactly one

LeetCode - Implement Magic Dictionary

Implement a magic directory with buildDict, and search methods. For the method buildDict, you'll be given a list of non-repetitive words to build a dictionary. For the method search, you'll be given a word, and judge whether if you modify exactly one

[leetcode-676-Implement Magic Dictionary]

Implement a magic directory with buildDict, and search methods. For the method buildDict, you'll be given a list of non-repetitive words to build a dictionary. For the method search, you'll be given a word, and judge whether if you modify exactly one

LC 470. Implement Rand10() Using Rand7()

Given a function rand7 which generates a uniform random integer in the range 1 to 7, write a function rand10 which generates a uniform random integer in the range 1 to 10. Do NOT use system's Math.random(). Example 1: Input: 1 Output: [7] Example 2:

[LC] 232. Implement Queue using Stacks

Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of queue. pop() -- Removes the element from in front of queue. peek() -- Get the front element. empty() -- Return whether the queue is empty. Example: M

[LC] 225. Implement Stack using Queues

Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. pop() -- Removes the element on top of the stack. top() -- Get the top element. empty() -- Return whether the stack is empty. Example: MyStack stack = n

LeetCode Problems List 题目汇总

No. Title Level Rate 1 Two Sum Medium 17.70% 2 Add Two Numbers Medium 21.10% 3 Longest Substring Without Repeating Characters Medium 20.60% 4 Median of Two Sorted Arrays Hard 17.40% 5 Longest Palindromic Substring Medium 20.70% 6 ZigZag Conversion Ea

Leetcode problems classified by company 题目按公司分类(Last updated: October 2, 2017)

Sorted by frequency of problems that appear in real interviews.Last updated: October 2, 2017Google (214)534 Design TinyURL388 Longest Absolute File Path683 K Empty Slots340 Longest Substring with At Most K Distinct Characters681 Next Closest Time482

【LeetCode】前缀树 trie(共14题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [208]Implement Trie (Prefix Tree) (2018年11月27日) 实现基本的 trie 树,包括 insert, search, startWith 操作等 api. 题解:<程序员代码面试指南>chp5, 最后一题. 里面讲了怎么实现.这个就看代码吧.没啥好说的了. 1 class Trie { 2 public: 3 /** Ini