| | 1 | | // -------------------------------------------------------------------------------- |
| | 2 | | // Copyright (C) 2025 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 class ImplementTrieArray : 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(1) |
| | 22 | | /// </summary> |
| | 23 | | /// <param name="word"></param> |
| | 24 | | public void Insert(string word) |
| 2 | 25 | | { |
| 10 | 26 | | var currentNode = word.Select(c => c - 'a') |
| 10 | 27 | | .Aggregate(_root, (current, index) => current.Nodes[index] ??= new Node()); |
| | 28 | |
|
| 2 | 29 | | currentNode.IsWord = true; |
| 2 | 30 | | } |
| | 31 | |
|
| | 32 | | /// <summary> |
| | 33 | | /// Time complexity - O(m), where m is the length of the word |
| | 34 | | /// Space complexity - O(1) |
| | 35 | | /// </summary> |
| | 36 | | /// <param name="word"></param> |
| | 37 | | /// <returns></returns> |
| | 38 | | public bool Search(string word) |
| 3 | 39 | | { |
| 3 | 40 | | var currentNode = Traverse(word); |
| | 41 | |
|
| 3 | 42 | | return currentNode is { IsWord: true }; |
| 3 | 43 | | } |
| | 44 | |
|
| | 45 | | /// <summary> |
| | 46 | | /// Time complexity - O(m), where m is the length of the prefix |
| | 47 | | /// Space complexity - O(1) |
| | 48 | | /// </summary> |
| | 49 | | /// <param name="prefix"></param> |
| | 50 | | /// <returns></returns> |
| | 51 | | public bool StartsWith(string prefix) |
| 1 | 52 | | { |
| 1 | 53 | | return Traverse(prefix) != null; |
| 1 | 54 | | } |
| | 55 | |
|
| | 56 | | private Node? Traverse(string str) |
| 4 | 57 | | { |
| 4 | 58 | | var currentNode = _root; |
| | 59 | |
|
| 54 | 60 | | foreach (var index in str.Select(c => c - 'a')) |
| 14 | 61 | | { |
| 14 | 62 | | currentNode = currentNode.Nodes[index]; |
| | 63 | |
|
| 14 | 64 | | if (currentNode == null) |
| 0 | 65 | | { |
| 0 | 66 | | return null; |
| | 67 | | } |
| 14 | 68 | | } |
| | 69 | |
|
| 4 | 70 | | return currentNode; |
| 4 | 71 | | } |
| | 72 | |
|
| | 73 | | private class Node |
| | 74 | | { |
| 28 | 75 | | public Node?[] Nodes { get; } = new Node['z' - 'a' + 1]; |
| | 76 | |
|
| 5 | 77 | | public bool IsWord { get; set; } |
| | 78 | | } |
| | 79 | | } |