| | | 1 | | // -------------------------------------------------------------------------------- |
| | | 2 | | // Copyright (C) 2026 Eugene Eremeev (also known as Yevhenii Yeriemeieiv). |
| | | 3 | | // All Rights Reserved. |
| | | 4 | | // -------------------------------------------------------------------------------- |
| | | 5 | | // This software is the confidential and proprietary information of Eugene Eremeev |
| | | 6 | | // (also known as Yevhenii Yeriemeieiv) ("Confidential Information"). You shall not |
| | | 7 | | // disclose such Confidential Information and shall use it only in accordance with |
| | | 8 | | // the terms of the license agreement you entered into with Eugene Eremeev (also |
| | | 9 | | // known as Yevhenii Yeriemeieiv). |
| | | 10 | | // -------------------------------------------------------------------------------- |
| | | 11 | | |
| | | 12 | | namespace LeetCode.Algorithms.ImplementTrie; |
| | | 13 | | |
| | | 14 | | /// <inheritdoc /> |
| | | 15 | | public sealed class ImplementTrieDictionary : IImplementTrie |
| | | 16 | | { |
| | 1 | 17 | | private readonly Node _root = new(); |
| | | 18 | | |
| | | 19 | | /// <summary> |
| | | 20 | | /// Time complexity - O(m), where m is the length of the word |
| | | 21 | | /// Space complexity - O(m), where m is the length of the word |
| | | 22 | | /// </summary> |
| | | 23 | | /// <param name="word"></param> |
| | | 24 | | public void Insert(string word) |
| | 2 | 25 | | { |
| | 2 | 26 | | var currentNode = _root; |
| | | 27 | | |
| | 22 | 28 | | foreach (var c in word) |
| | 8 | 29 | | { |
| | 8 | 30 | | if (currentNode.Nodes.TryGetValue(c, out var node)) |
| | 3 | 31 | | { |
| | 3 | 32 | | currentNode = node; |
| | 3 | 33 | | } |
| | | 34 | | else |
| | 5 | 35 | | { |
| | 5 | 36 | | currentNode.Nodes[c] = new Node(); |
| | | 37 | | |
| | 5 | 38 | | currentNode = currentNode.Nodes[c]; |
| | 5 | 39 | | } |
| | 8 | 40 | | } |
| | | 41 | | |
| | 2 | 42 | | currentNode.IsWord = true; |
| | 2 | 43 | | } |
| | | 44 | | |
| | | 45 | | /// <summary> |
| | | 46 | | /// Time complexity - O(m), where m is the length of the word |
| | | 47 | | /// Space complexity - O(1) |
| | | 48 | | /// </summary> |
| | | 49 | | /// <param name="word"></param> |
| | | 50 | | /// <returns></returns> |
| | | 51 | | public bool Search(string word) |
| | 3 | 52 | | { |
| | 3 | 53 | | var currentNode = Traverse(word); |
| | | 54 | | |
| | 3 | 55 | | return currentNode is { IsWord: true }; |
| | 3 | 56 | | } |
| | | 57 | | |
| | | 58 | | /// <summary> |
| | | 59 | | /// Time complexity - O(m), where m is the length of the prefix |
| | | 60 | | /// Space complexity - O(1) |
| | | 61 | | /// </summary> |
| | | 62 | | /// <param name="prefix"></param> |
| | | 63 | | /// <returns></returns> |
| | | 64 | | public bool StartsWith(string prefix) |
| | 1 | 65 | | { |
| | 1 | 66 | | return Traverse(prefix) != null; |
| | 1 | 67 | | } |
| | | 68 | | |
| | | 69 | | private Node? Traverse(string str) |
| | 4 | 70 | | { |
| | 4 | 71 | | var currentNode = _root; |
| | | 72 | | |
| | 40 | 73 | | foreach (var c in str) |
| | 14 | 74 | | { |
| | 14 | 75 | | if (!currentNode.Nodes.TryGetValue(c, out var node)) |
| | 0 | 76 | | { |
| | 0 | 77 | | return null; |
| | | 78 | | } |
| | | 79 | | |
| | 14 | 80 | | currentNode = node; |
| | 14 | 81 | | } |
| | | 82 | | |
| | 4 | 83 | | return currentNode; |
| | 4 | 84 | | } |
| | | 85 | | |
| | | 86 | | private class Node |
| | | 87 | | { |
| | 38 | 88 | | public Dictionary<char, Node> Nodes { get; } = new(); |
| | | 89 | | |
| | 5 | 90 | | public bool IsWord { get; set; } |
| | | 91 | | } |
| | | 92 | | } |