<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Fomagran on Medium]]></title>
        <description><![CDATA[Stories by Fomagran on Medium]]></description>
        <link>https://medium.com/@fomagran?source=rss-5d7fe845ce73------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*Ceo_ScfSEkIrVE7mS4fxmg.jpeg</url>
            <title>Stories by Fomagran on Medium</title>
            <link>https://medium.com/@fomagran?source=rss-5d7fe845ce73------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 06:46:28 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@fomagran/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[[Swift] LeetCode Design Add and Search Words Data Structure]]></title>
            <link>https://medium.com/@fomagran/swift-leetcode-design-add-and-search-words-data-structure-7fa115772a54?source=rss-5d7fe845ce73------2</link>
            <guid isPermaLink="false">https://medium.com/p/7fa115772a54</guid>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[algorithms]]></category>
            <category><![CDATA[leetcode]]></category>
            <category><![CDATA[trie]]></category>
            <dc:creator><![CDATA[Fomagran]]></dc:creator>
            <pubDate>Tue, 18 Oct 2022 02:03:37 GMT</pubDate>
            <atom:updated>2022-10-19T03:14:17.200Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/312/1*P60QKyMfAzIogKok7hn40g.jpeg" /></figure><p><strong>Problem</strong></p><p>Related Topic: Trie</p><p>Difficulty: Medium</p><p><a href="https://leetcode.com/problems/design-add-and-search-words-data-structure/">Design Add and Search Words Data Structure - LeetCode</a></p><p><strong>Solution</strong></p><ol><li>Define Trie node class.</li></ol><p>First of all, we need to define Trie node class because the Word Dictionary class is almost the same as Trie data structure.</p><p>Trie node needs two properties “children” and “isEnd”.</p><p>“children” property type is going to be a dictionary with a character as a key and a trie node as a value.</p><p>“isEnd” property type is going to be boolean because it designates if the node is the end of a word.</p><p>2. Implement the addword() method</p><p>In order to add a word to Trie, we need a root node of Trie.</p><p>By iterating through every single character in a word, we’re going to check if the node’s children have a character from the root.</p><p>If the children don’t have a character, we’ll insert a trie node corresponding to the character.</p><p>And then, we’re going to update the node pointer to this new trie node.</p><p>When the iterating is over, what we’re going to do at the end is update the node’s “isEnd” to true.</p><p>3. Implement the search() method</p><p>To implement this method, we’ll need a queue because we’re going to do the BFS.</p><p>So the first thing we’re going to do is add the root node to the queue.</p><p>After that, we’ll iterate through every single character in a word again as the addWord() method.</p><p>And then, we have to check if the character is a dot or not.</p><p>The reason we’re checking it is that if the character is a dot, that means potentially it can go down 26 different paths, otherwise it only can go down the corresponding character.</p><p>So in the case of a dot, we’re going to set the queue to all children of the current level of trie.</p><p>If it’s one of the alphabets, we’re going to set the queue to only children of the corresponding character.</p><p>When the iterating is over, we’re just going to return the trie nodes in the queue containing “isEnd” or not.</p><p><strong>Submission Detail</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/869/1*WjkkdQG8tWeOrBRBMEND1Q.png" /></figure><p>Constraints</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/461/1*24hjxtanVdK9uNPEM6khZA.png" /></figure><p><strong>Time Complexity</strong></p><p>addWord(): O(n), where n is the length of a word.</p><p>search(): O(26³ + n-3), where n is the length of a word.</p><p>As we can see the constraints, “there will be at most 3 dots in a word for search queries”.</p><p>If the character is a dot, it can have 26 cases (the number of lowercase English letters).</p><p>Therefore, if a word has three dots, it takes up to 26³ and also additionally takes the word’s length — 3(number of dots).</p><p><strong>Space Complexity</strong></p><p>O(10⁴ * 25)</p><p>As we can see the constraints, there are “At most 10⁴ calls will be made to addWord() and search” and “1 ≤ word.length ≤ 25”.</p><p>In the worst case, we can add 10⁴ words with a maximum of 25 characters.</p><p>(In other words, O(m) where m is the sum of length of all words)</p><h3><strong>Source Code</strong></h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a0a59830c82651b6a1e443dd9601be8f/href">https://medium.com/media/a0a59830c82651b6a1e443dd9601be8f/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7fa115772a54" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>