{"id":1074,"date":"2025-09-01T22:53:38","date_gmt":"2025-09-01T17:23:38","guid":{"rendered":"https:\/\/codeanddebug.in\/blog\/?p=1074"},"modified":"2025-09-01T22:53:39","modified_gmt":"2025-09-01T17:23:39","slug":"implement-trie-ll","status":"publish","type":"post","link":"https:\/\/codeanddebug.in\/blog\/implement-trie-ll\/","title":{"rendered":"Implement Trie ll | Full Trie with Insert, Count, and Erase"},"content":{"rendered":"\n<p>The <strong>Implement Trie II<\/strong> problem is an extension of the basic <strong>Implement Trie (Prefix Tree)<\/strong>. In addition to inserting words and checking prefixes, this version requires advanced operations like:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Insert(word):<\/strong> Insert a word into the Trie.<\/li>\n\n\n\n<li><strong>countWordsEqualTo(word):<\/strong> Return the number of times a given word has been inserted.<\/li>\n\n\n\n<li><strong>countWordsStartingWith(prefix):<\/strong> Return the number of words that start with the given prefix.<\/li>\n\n\n\n<li><strong>erase(word):<\/strong> Remove one occurrence of the word from the Trie.<\/li>\n<\/ol>\n\n\n\n<p>This extended Trie allows handling multiple occurrences of the same word and gives more control over word counts.<\/p>\n\n\n\n<p>Here&#8217;s the [<strong><a href=\"https:\/\/www.naukri.com\/code360\/problems\/implement-trie_1387095?leftPanelTabValue=PROBLEM\" target=\"_blank\" rel=\"noreferrer noopener\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\"><span style=\"text-decoration: underline;\">Problem Link<\/span><\/mark><\/a><\/strong>] to begin with.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.5rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"Operations: \ninsert(&quot;apple&quot;)\ninsert(&quot;apple&quot;)\ncountWordsEqualTo(&quot;apple&quot;)   \u2192 2\ncountWordsStartingWith(&quot;app&quot;) \u2192 2\nerase(&quot;apple&quot;)\ncountWordsEqualTo(&quot;apple&quot;)   \u2192 1\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #569CD6\">Operations<\/span><span style=\"color: #D4D4D4\">: <\/span><\/span>\n<span class=\"line\"><span style=\"color: #CE9178\">insert(&quot;apple&quot;)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #CE9178\">insert(&quot;apple&quot;)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #CE9178\">countWordsEqualTo(&quot;apple&quot;)   \u2192 2<\/span><\/span>\n<span class=\"line\"><span style=\"color: #CE9178\">countWordsStartingWith(&quot;app&quot;) \u2192 2<\/span><\/span>\n<span class=\"line\"><span style=\"color: #CE9178\">erase(&quot;apple&quot;)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #CE9178\">countWordsEqualTo(&quot;apple&quot;)   \u2192 1<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>Explanation:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>\"apple\"<\/code> was inserted twice.<\/li>\n\n\n\n<li>Both words share prefix <code>\"app\"<\/code>.<\/li>\n\n\n\n<li>After erasing one <code>\"apple\"<\/code>, only one remains.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Intuition and Approach<\/h2>\n\n\n\n<p>To implement <strong>Implement Trie II<\/strong>, we extend the traditional Trie by maintaining two counters at each node:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>count_prefix:<\/strong>\n<ul class=\"wp-block-list\">\n<li>Counts how many words pass through this node.<\/li>\n\n\n\n<li>Useful for <code>countWordsStartingWith<\/code>.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>end_count:<\/strong>\n<ul class=\"wp-block-list\">\n<li>Counts how many words end at this node.<\/li>\n\n\n\n<li>Useful for <code>countWordsEqualTo<\/code>.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Operations<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Insert(word):<\/strong>\n<ul class=\"wp-block-list\">\n<li>Traverse each character.<\/li>\n\n\n\n<li>If the character path doesn\u2019t exist, create a new node.<\/li>\n\n\n\n<li>Increment <code>count_prefix<\/code> at every node visited.<\/li>\n\n\n\n<li>At the final node, increment <code>end_count<\/code>.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>countWordsEqualTo(word):<\/strong>\n<ul class=\"wp-block-list\">\n<li>Traverse characters of the word.<\/li>\n\n\n\n<li>If a character is missing, return 0.<\/li>\n\n\n\n<li>At the end, return <code>end_count<\/code> of the final node.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>countWordsStartingWith(prefix):<\/strong>\n<ul class=\"wp-block-list\">\n<li>Traverse prefix characters.<\/li>\n\n\n\n<li>If a character is missing, return 0.<\/li>\n\n\n\n<li>Return <code>count_prefix<\/code> of the final prefix node.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>erase(word):<\/strong>\n<ul class=\"wp-block-list\">\n<li>Traverse the word\u2019s path.<\/li>\n\n\n\n<li>Decrease <code>count_prefix<\/code> at each node.<\/li>\n\n\n\n<li>Decrease <code>end_count<\/code> at the final node.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Code Implementation<\/h2>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.5rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"from os import *\nfrom sys import *\nfrom collections import *\nfrom math import *\n\nclass TrieNode:\n    def __init__(self):\n        self.children = {}\n        self.count_prefix = 0\n        self.end_count = 0\n\nclass Trie:\n    def __init__(self):\n        self.root = TrieNode()\n\n    def insert(self, word):\n        node = self.root\n        for ch in word:\n            if ch not in node.children:\n                node.children[ch] = TrieNode()\n            node = node.children[ch]\n            node.count_prefix += 1\n        node.end_count += 1\n\n    def countWordsEqualTo(self, word):\n        node = self.root\n        for ch in word:\n            if ch not in node.children:\n                return 0\n            node = node.children[ch]\n        return node.end_count\n\n    def countWordsStartingWith(self, word):\n        node = self.root\n        for ch in word:\n            if ch not in node.children:\n                return 0\n            node = node.children[ch]\n        return node.count_prefix\n\n    def erase(self, word):\n        node = self.root\n        for ch in word:\n            if ch not in node.children:\n                return\n            node = node.children[ch]\n            node.count_prefix -= 1\n        node.end_count -= 1\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #C586C0\">from<\/span><span style=\"color: #D4D4D4\"> os <\/span><span style=\"color: #C586C0\">import<\/span><span style=\"color: #D4D4D4\"> *<\/span><\/span>\n<span class=\"line\"><span style=\"color: #C586C0\">from<\/span><span style=\"color: #D4D4D4\"> sys <\/span><span style=\"color: #C586C0\">import<\/span><span style=\"color: #D4D4D4\"> *<\/span><\/span>\n<span class=\"line\"><span style=\"color: #C586C0\">from<\/span><span style=\"color: #D4D4D4\"> collections <\/span><span style=\"color: #C586C0\">import<\/span><span style=\"color: #D4D4D4\"> *<\/span><\/span>\n<span class=\"line\"><span style=\"color: #C586C0\">from<\/span><span style=\"color: #D4D4D4\"> math <\/span><span style=\"color: #C586C0\">import<\/span><span style=\"color: #D4D4D4\"> *<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #569CD6\">class<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4EC9B0\">TrieNode<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">__init__<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #569CD6\">self<\/span><span style=\"color: #D4D4D4\">.children = {}<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #569CD6\">self<\/span><span style=\"color: #D4D4D4\">.count_prefix = <\/span><span style=\"color: #B5CEA8\">0<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #569CD6\">self<\/span><span style=\"color: #D4D4D4\">.end_count = <\/span><span style=\"color: #B5CEA8\">0<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #569CD6\">class<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4EC9B0\">Trie<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">__init__<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #569CD6\">self<\/span><span style=\"color: #D4D4D4\">.root = TrieNode()<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">insert<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">word<\/span><span style=\"color: #D4D4D4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        node = <\/span><span style=\"color: #569CD6\">self<\/span><span style=\"color: #D4D4D4\">.root<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">for<\/span><span style=\"color: #D4D4D4\"> ch <\/span><span style=\"color: #C586C0\">in<\/span><span style=\"color: #D4D4D4\"> word:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> ch <\/span><span style=\"color: #569CD6\">not<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">in<\/span><span style=\"color: #D4D4D4\"> node.children:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                node.children[ch] = TrieNode()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            node = node.children[ch]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            node.count_prefix += <\/span><span style=\"color: #B5CEA8\">1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        node.end_count += <\/span><span style=\"color: #B5CEA8\">1<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">countWordsEqualTo<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">word<\/span><span style=\"color: #D4D4D4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        node = <\/span><span style=\"color: #569CD6\">self<\/span><span style=\"color: #D4D4D4\">.root<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">for<\/span><span style=\"color: #D4D4D4\"> ch <\/span><span style=\"color: #C586C0\">in<\/span><span style=\"color: #D4D4D4\"> word:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> ch <\/span><span style=\"color: #569CD6\">not<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">in<\/span><span style=\"color: #D4D4D4\"> node.children:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #B5CEA8\">0<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            node = node.children[ch]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> node.end_count<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">countWordsStartingWith<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">word<\/span><span style=\"color: #D4D4D4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        node = <\/span><span style=\"color: #569CD6\">self<\/span><span style=\"color: #D4D4D4\">.root<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">for<\/span><span style=\"color: #D4D4D4\"> ch <\/span><span style=\"color: #C586C0\">in<\/span><span style=\"color: #D4D4D4\"> word:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> ch <\/span><span style=\"color: #569CD6\">not<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">in<\/span><span style=\"color: #D4D4D4\"> node.children:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #B5CEA8\">0<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            node = node.children[ch]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> node.count_prefix<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">erase<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">word<\/span><span style=\"color: #D4D4D4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        node = <\/span><span style=\"color: #569CD6\">self<\/span><span style=\"color: #D4D4D4\">.root<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">for<\/span><span style=\"color: #D4D4D4\"> ch <\/span><span style=\"color: #C586C0\">in<\/span><span style=\"color: #D4D4D4\"> word:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> ch <\/span><span style=\"color: #569CD6\">not<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">in<\/span><span style=\"color: #D4D4D4\"> node.children:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                <\/span><span style=\"color: #C586C0\">return<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            node = node.children[ch]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            node.count_prefix -= <\/span><span style=\"color: #B5CEA8\">1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        node.end_count -= <\/span><span style=\"color: #B5CEA8\">1<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Code Explanation<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>TrieNode<\/code>:<\/strong>\n<ul class=\"wp-block-list\">\n<li><code>children<\/code>: stores next characters.<\/li>\n\n\n\n<li><code>count_prefix<\/code>: counts how many words include this node in their path.<\/li>\n\n\n\n<li><code>end_count<\/code>: counts how many words end here.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong><code>insert<\/code>:<\/strong> Updates prefix counts along the path and increments end count.<\/li>\n\n\n\n<li><strong><code>countWordsEqualTo<\/code>:<\/strong> Returns how many words exactly match.<\/li>\n\n\n\n<li><strong><code>countWordsStartingWith<\/code>:<\/strong> Returns how many words share a given prefix.<\/li>\n\n\n\n<li><strong><code>erase<\/code>:<\/strong> Decreases prefix counts and word end counts to remove one occurrence.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Dry Run<\/h2>\n\n\n\n<p>Operations:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.5rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"insert(&quot;apple&quot;)\ninsert(&quot;apple&quot;)\ncountWordsEqualTo(&quot;apple&quot;)\ncountWordsStartingWith(&quot;app&quot;)\nerase(&quot;apple&quot;)\ncountWordsEqualTo(&quot;apple&quot;)\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D4D4D4\">insert(<\/span><span style=\"color: #CE9178\">&quot;apple&quot;<\/span><span style=\"color: #D4D4D4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">insert(<\/span><span style=\"color: #CE9178\">&quot;apple&quot;<\/span><span style=\"color: #D4D4D4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">countWordsEqualTo(<\/span><span style=\"color: #CE9178\">&quot;apple&quot;<\/span><span style=\"color: #D4D4D4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">countWordsStartingWith(<\/span><span style=\"color: #CE9178\">&quot;app&quot;<\/span><span style=\"color: #D4D4D4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">erase(<\/span><span style=\"color: #CE9178\">&quot;apple&quot;<\/span><span style=\"color: #D4D4D4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">countWordsEqualTo(<\/span><span style=\"color: #CE9178\">&quot;apple&quot;<\/span><span style=\"color: #D4D4D4\">)<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>Steps:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Insert <code>\"apple\"<\/code> twice \u2192 <code>end_count(\"e\") = 2<\/code>, prefix counts updated along path.<\/li>\n\n\n\n<li><code>countWordsEqualTo(\"apple\")<\/code> \u2192 2.<\/li>\n\n\n\n<li><code>countWordsStartingWith(\"app\")<\/code> \u2192 2.<\/li>\n\n\n\n<li>Erase <code>\"apple\"<\/code> \u2192 <code>end_count(\"e\") = 1<\/code>, prefix counts decremented.<\/li>\n\n\n\n<li><code>countWordsEqualTo(\"apple\")<\/code> \u2192 1.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Time and Space Complexity<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Insert:<\/strong> O(L), where L = length of word.<\/li>\n\n\n\n<li><strong>countWordsEqualTo:<\/strong> O(L).<\/li>\n\n\n\n<li><strong>countWordsStartingWith:<\/strong> O(P), where P = prefix length.<\/li>\n\n\n\n<li><strong>erase:<\/strong> O(L).<\/li>\n\n\n\n<li><strong>Space Complexity:<\/strong> O(N * L), where N = number of words, L = average length of words.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>The <strong>Implement Trie II<\/strong> problem enhances the basic Trie by adding advanced operations like counting exact words, counting prefixes, and erasing occurrences.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Supports efficient <code>insert<\/code>, <code>search<\/code>, <code>count<\/code>, and <code>erase<\/code>.<\/li>\n\n\n\n<li>Useful in text editors, autocomplete, spell checkers, and dictionaries.<\/li>\n\n\n\n<li>Runs in <strong>O(L) per operation<\/strong> with scalable memory usage.<\/li>\n<\/ul>\n\n\n\n<p>This makes it one of the most powerful variations of Trie for interview preparation.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<div class=\"wp-block-buttons is-content-justification-center is-layout-flex wp-container-core-buttons-is-layout-16018d1d wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/codeanddebug.in\/course\/zero-to-hero-python-dsa\" target=\"_blank\" rel=\"noreferrer noopener\">Join our Advance DSA COURSE<\/a><\/div>\n<\/div>\n\n\n\n<p><em>For any changes to the article, kindly email at <a href=\"mailto:code@codeanddebug.in\" target=\"_blank\" rel=\"noreferrer noopener\">code@codeanddebug.in<\/a> or contact us at <a href=\"tel:+91-9712928220\" target=\"_blank\" rel=\"noreferrer noopener\">+91-9712928220<\/a>.<\/em><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Implement Trie II problem is an extension of the basic Implement Trie (Prefix Tree). In addition to inserting words and checking prefixes, this version requires advanced operations like: This extended Trie allows handling multiple occurrences of the same word and gives more control over word counts. Here&#8217;s the [Problem Link] to begin with. Example<\/p>\n","protected":false},"author":1,"featured_media":1075,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,6],"tags":[19,44],"class_list":{"0":"post-1074","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-data-structures-and-algorithm","8":"category-intermediate","9":"tag-medium","10":"tag-tries"},"featured_image_src":"https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/09\/implement-trie-2-featured-image.png","author_info":{"display_name":"codeanddebug","author_link":"https:\/\/codeanddebug.in\/blog\/author\/codeanddebug\/"},"_links":{"self":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/1074","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/comments?post=1074"}],"version-history":[{"count":1,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/1074\/revisions"}],"predecessor-version":[{"id":1076,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/1074\/revisions\/1076"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media\/1075"}],"wp:attachment":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media?parent=1074"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/categories?post=1074"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/tags?post=1074"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}