LeetCode: 211. Design Add and Search Words Data Structure (and others)

// Solution @ Sergey Leschev

class TrieNode {
var children: [Character: TrieNode]
var isWord: Bool

init() {
children = [:]
isWord = false
}
}

class WordDictionary {
var root: TrieNode

init() {
root =…


This content originally appeared on DEV Community and was authored by Sergey Leschev

// Solution @ Sergey Leschev

class TrieNode {
    var children: [Character: TrieNode]
    var isWord: Bool

    init() {
        children = [:]
        isWord = false
    }
}

class WordDictionary {
    var root: TrieNode

    init() {
        root = TrieNode()
    }

    func addWord(_ word: String) {
        var node = root
        for c in word {
            if let child = node.children[c] {
                node = child
            } else {
                let child = TrieNode()
                node.children[c] = child
                node = child
            }
        }
        node.isWord = true
    }

    func search(_ word: String) -> Bool {
        return searchHelper(Array(word), 0, root)
    }

    private func searchHelper(_ word: [Character], _ index: Int, _ node: TrieNode) -> Bool {
        if index == word.count {
            return node.isWord
        }

        let c = word[index]

        if c != "." {
            if let child = node.children[c] {
                return searchHelper(word, index+1, child)
            }
            return false
        } else {
            for child in node.children.values {
                if searchHelper(word, index+1, child) {
                    return true
                }
            }
            return false
        }
    }
}

My LeetCode Solutions / Swift
https://github.com/sergeyleschev/leetcode-swift

My LeetCode Solutions / TypeScript
https://github.com/sergeyleschev/leetcode-typescript

Contacts
I have a clear focus on time-to-market and don't prioritize technical debt. And I took part in the Pre-Sale/RFX activity as a System Architect, assessment efforts for Mobile (iOS-Swift, Android-Kotlin), Frontend (React-TypeScript) and Backend (NodeJS-.NET-PHP-Kafka-SQL-NoSQL). And I also formed the work of Pre-Sale as a CTO from Opportunity to Proposal via knowledge transfer to Successful Delivery.

🛩️ #startups #management #cto #swift #typescript #database
đź“§ Email: sergey.leschev@gmail.com
đź‘‹ LinkedIn: https://www.linkedin.com/in/sergeyleschev/
đź‘‹ LeetCode: https://leetcode.com/sergeyleschev/
đź‘‹ Twitter: https://twitter.com/sergeyleschev
đź‘‹ Github: https://github.com/sergeyleschev
🌎 Website: https://sergeyleschev.github.io


This content originally appeared on DEV Community and was authored by Sergey Leschev


Print Share Comment Cite Upload Translate Updates
APA

Sergey Leschev | Sciencx (2023-03-19T07:24:54+00:00) LeetCode: 211. Design Add and Search Words Data Structure (and others). Retrieved from https://www.scien.cx/2023/03/19/leetcode-211-design-add-and-search-words-data-structure-and-others/

MLA
" » LeetCode: 211. Design Add and Search Words Data Structure (and others)." Sergey Leschev | Sciencx - Sunday March 19, 2023, https://www.scien.cx/2023/03/19/leetcode-211-design-add-and-search-words-data-structure-and-others/
HARVARD
Sergey Leschev | Sciencx Sunday March 19, 2023 » LeetCode: 211. Design Add and Search Words Data Structure (and others)., viewed ,<https://www.scien.cx/2023/03/19/leetcode-211-design-add-and-search-words-data-structure-and-others/>
VANCOUVER
Sergey Leschev | Sciencx - » LeetCode: 211. Design Add and Search Words Data Structure (and others). [Internet]. [Accessed ]. Available from: https://www.scien.cx/2023/03/19/leetcode-211-design-add-and-search-words-data-structure-and-others/
CHICAGO
" » LeetCode: 211. Design Add and Search Words Data Structure (and others)." Sergey Leschev | Sciencx - Accessed . https://www.scien.cx/2023/03/19/leetcode-211-design-add-and-search-words-data-structure-and-others/
IEEE
" » LeetCode: 211. Design Add and Search Words Data Structure (and others)." Sergey Leschev | Sciencx [Online]. Available: https://www.scien.cx/2023/03/19/leetcode-211-design-add-and-search-words-data-structure-and-others/. [Accessed: ]
rf:citation
» LeetCode: 211. Design Add and Search Words Data Structure (and others) | Sergey Leschev | Sciencx | https://www.scien.cx/2023/03/19/leetcode-211-design-add-and-search-words-data-structure-and-others/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.