首页 > 其他分享 >208.实现Trie(前缀树)

208.实现Trie(前缀树)

时间:2022-11-11 00:33:48浏览次数:48  
标签:node insert search word 前缀 Trie 208 prefix

Trie(发音类似 "try")或者说 前缀树 是一种树形数据结构,用于高效地存储和检索字符串数据集中的键。这一数据结构有相当多的应用情景,例如自动补完和拼写检查。

请你实现 Trie 类:

  • Trie() 初始化前缀树对象。
  • void insert(String word) 向前缀树中插入字符串 word 。
  • boolean search(String word) 如果字符串 word 在前缀树中,返回 true(即,在检索之前已经插入);否则,返回 false 。
  • boolean startsWith(String prefix) 如果之前已经插入的字符串 word 的前缀之一为 prefix ,返回 true ;否则,返回 false 

示例:

输入
["Trie", "insert", "search", "search", "startsWith", "insert", "search"]
[[], ["apple"], ["apple"], ["app"], ["app"], ["app"], ["app"]]
输出
[null, null, true, false, true, null, true]

解释
Trie trie = new Trie();
trie.insert("apple");
trie.search("apple");   // 返回 True
trie.search("app");     // 返回 False
trie.startsWith("app"); // 返回 True
trie.insert("app");
trie.search("app");     // 返回 True

提示:

  • 1 <= word.length, prefix.length <= 2000
  • word 和 prefix 仅由小写英文字母组成
  • insertsearch 和 startsWith 调用次数 总计 不超过 3 * 104 次

方法:字典树

时间复杂度:初始化为O(1),其余操作为O(|S|),其中|S|是每次插入或查询的字符串的长度

空间复杂度:O(∣T∣⋅Σ),其中 ∣T∣为所有插入字符串的长度之和,Σ 为字符集的大小,本题 Σ=26。

 1 var Trie = function () {
 2     this.children = {};
 3 };
 4 
 5 /** 
 6  * @param {string} word
 7  * @return {void}
 8  */
 9 Trie.prototype.insert = function (word) {
10     let node = this.children;
11     for (const ch of word) {
12         if (!node[ch]) {
13             node[ch] = {};
14         }
15         node = node[ch];
16     }
17     node.isEnd = true;
18 };
19 
20 /** 
21  * @param {string} word
22  * @return {boolean}
23  */
24 Trie.prototype.searchPrefix = function (prefix) {
25     let node = this.children;
26     for (const ch of prefix) {
27         if (!node[ch]) {
28             return false;
29         }
30         node = node[ch];
31     }
32     return node;
33 }
34 Trie.prototype.search = function (word) {
35     const node = this.searchPrefix(word);
36     return node !== undefined && node.isEnd !== undefined;
37 };
38 
39 /** 
40  * @param {string} prefix
41  * @return {boolean}
42  */
43 Trie.prototype.startsWith = function (prefix) {
44     return this.searchPrefix(prefix);
45 };
46 
47 /**
48  * Your Trie object will be instantiated and called as such:
49  * var obj = new Trie()
50  * obj.insert(word)
51  * var param_2 = obj.search(word)
52  * var param_3 = obj.startsWith(prefix)
53  */

 

标签:node,insert,search,word,前缀,Trie,208,prefix
From: https://www.cnblogs.com/icyyyy/p/16879337.html

相关文章

  • 每周一脚本:批量对多个文件增加前缀
    最近从设计师那里get了超多的图,结果都是1.png,2.png这样的文件名,自己还需要将这些文件变成可读的文件名,不想一个一个得修改,于是就写了一个简单的脚本,实现批量对多个文件增加......
  • 查找字符串数组中的最长公共前缀
     import java.util.*;public class Solution {    /**     *      * @param strs string字符串一维数组      * @return string......
  • leetcode-2089-easy
    FindTargetIndicesAfterSortingArrayYouaregivena0-indexedintegerarraynumsandatargetelementtarget.Atargetindexisanindexisuchthatnums[......
  • n维前缀和
    n维前缀和DP法每一维考虑,从低维向高维转移。#include<bits/stdc++.h>usingnamespacestd;#defineLLlonglong#defineendl'\n'constintMAXN=1e5;constin......
  • 20220802 鸟哥 Linux 私房菜【归档】
    参考资料鸟哥Linux私房菜-基础学习篇(第四版)鳥哥私房菜-基礎學習篇目錄-forCentOS7环境信息CentOS7.x书中使用版本:7.1练习使用版本:7.9目录00.计算......
  • 20220804 02. 主机规划与磁盘分区
    2.1Linux与硬件的搭配虽然个人计算机各元件的主要接口是大同小异的,包括前面第零章计算机概论讲到的种种接口等,但是由于新的技术来得太快,Linux核心针对新硬件所纳入的驱......
  • 20220803 01. Linux是什么与如何学习
    1.1Linux是什么Linux就是一组软件1.1.1Linux是什么?操作系统/应用程序?Linux就是一套操作系统Linux就是核心与系统调用接口那两层早期的Linux是针对386来开发的Tor......
  • 20220807 04. 首次登陆与线上求助
    4.1首次登陆系统s一般来说,我们不建议您直接使用root的身份登陆系统喔!请使用一般帐号登陆!等到有需要修改或者是创建系统相关的管理工作时,才切换身份成为root!为什么......
  • 20220810 06. Linux 文件与目录管理
    6.1目录与路径6.1.1相对路径与绝对路径路径(PATH)绝对路径:路径的写法“一定由根目录/写起”,例如:/usr/share/doc这个目录。相对路径:路径的写法“不是由/写起......
  • 20220818 09. vim 程序编辑器
    在所有的Linuxdistributions上头都会有的一套文书编辑器就是vi,而且很多软件默认也是使用vi做为他们编辑的接口,因此鸟哥建议您务必要学会使用vi这个正规的文书编......