zoukankan      html  css  js  c++  java
  • 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.


    1. /**
    2. * Initialize your data structure here.
    3. */
    4. var MagicDictionary = function () {
    5. this.words = new Set();
    6. };
    7. /**
    8. * Build a dictionary through a list of words
    9. * @param {string[]} dict
    10. * @return {void}
    11. */
    12. MagicDictionary.prototype.buildDict = function (dict) {
    13. for (var i in dict) {
    14. this.words.add(dict[i]);
    15. }
    16. };
    17. /**
    18. * Returns if there is any word in the trie that equals to the given word after modifying exactly one character
    19. * @param {string} word
    20. * @return {boolean}
    21. */
    22. MagicDictionary.prototype.search = function (word) {
    23. for (let i = 0; i < word.length; i++) {
    24. for (let code = 65; code < 91; code++) {
    25. let c = String.fromCharCode(code).toLowerCase();
    26. if (word[i] == c) continue;
    27. let str = word.slice(0, i) + c + word.slice(i + 1, word.length);
    28. if (this.words.has(str)) return true;
    29. }
    30. }
    31. return false;
    32. };
    33. let func = ["MagicDictionary", "buildDict", "search", "search", "search", "search"];
    34. let arg = [[], [["hello", "leetcode"]], ["hello"], ["hhllo"], ["hell"], ["leetcoded"]];
    35. let dict = new MagicDictionary();
    36. for (var i = 1; i < func.length; i++) {
    37. console.log(dict[func[i]](arg[i][0]));
    38. }
    39. /**
    40. * Your MagicDictionary object will be instantiated and called as such:
    41. * var obj = Object.create(MagicDictionary).createNew()
    42. * obj.buildDict(dict)
    43. * var param_2 = obj.search(word)
    44. */







  • 相关阅读:
    kittle入门之文本文件导入数据库
    关于java的杂乱无章(续更)
    SpringBoot+Mybatis+redis实现二级缓存
    Spring AOP面向切面编程,监听某个方法
    xxl-job编写GULE(Java)运行模式,带执行参数(url)
    java后台解决跨域问题
    Spring Boot+JWT+Spring Security实现授权认证保护Rest API
    Java中的23种设计模式之——访问者(Visitor)模式(7)
    Java中的23种设计模式之——策略(Stragedy)模式(6)
    Java中的23种设计模式之——生成器(Builder)模式(5)
  • 原文地址:https://www.cnblogs.com/xiejunzhao/p/8040329.html
Copyright © 2011-2022 走看看