| | 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.ReplaceWords; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public class ReplaceWordsTrieNode : IReplaceWords |
| | 16 | | { |
| | 17 | | /// <summary> |
| | 18 | | /// Time complexity - O(n * L + k * L), where n is the number of words in the dictionary, L is the length of the |
| | 19 | | /// k is the number of words in the sentence |
| | 20 | | /// Space complexity - O(n * L + m), where n is the number of words in the dictionary, L is the length of the wo |
| | 21 | | /// is the length of the sentence |
| | 22 | | /// </summary> |
| | 23 | | /// <param name="dictionary"></param> |
| | 24 | | /// <param name="sentence"></param> |
| | 25 | | /// <returns></returns> |
| | 26 | | public string ReplaceWords(IList<string> dictionary, string sentence) |
| 6 | 27 | | { |
| 6 | 28 | | var trie = new Trie(dictionary); |
| | 29 | |
|
| 6 | 30 | | var words = sentence.Split(' '); |
| | 31 | |
|
| 68 | 32 | | for (var i = 0; i < words.Length; i++) |
| 28 | 33 | | { |
| 28 | 34 | | var word = words[i]; |
| | 35 | |
|
| 28 | 36 | | var current = trie.Root; |
| | 37 | |
|
| 219 | 38 | | foreach (var c in word.TakeWhile(c => current.Children.ContainsKey(c) && current.Word == null)) |
| 37 | 39 | | { |
| 37 | 40 | | current = current.Children[c]; |
| 37 | 41 | | } |
| | 42 | |
|
| 28 | 43 | | if (current.Word != null) |
| 19 | 44 | | { |
| 19 | 45 | | words[i] = current.Word; |
| 19 | 46 | | } |
| 28 | 47 | | } |
| | 48 | |
|
| 6 | 49 | | return string.Join(' ', words); |
| 6 | 50 | | } |
| | 51 | |
|
| | 52 | | private class Trie |
| | 53 | | { |
| 6 | 54 | | public Trie(IEnumerable<string> words) |
| 6 | 55 | | { |
| 6 | 56 | | AddRange(words); |
| 6 | 57 | | } |
| | 58 | |
|
| 51 | 59 | | public TrieNode Root { get; } = new(); |
| | 60 | |
|
| | 61 | | private void Add(string word) |
| 17 | 62 | | { |
| 17 | 63 | | var node = Root; |
| | 64 | |
|
| 159 | 65 | | foreach (var c in word) |
| 54 | 66 | | { |
| 54 | 67 | | if (!node.Children.TryGetValue(c, out var value)) |
| 35 | 68 | | { |
| 35 | 69 | | value = new TrieNode(); |
| | 70 | |
|
| 35 | 71 | | node.Children[c] = value; |
| 35 | 72 | | } |
| | 73 | |
|
| 54 | 74 | | node = value; |
| 54 | 75 | | } |
| | 76 | |
|
| 17 | 77 | | node.Word = word; |
| 17 | 78 | | } |
| | 79 | |
|
| | 80 | | private void AddRange(IEnumerable<string> words) |
| 6 | 81 | | { |
| 52 | 82 | | foreach (var word in words) |
| 17 | 83 | | { |
| 17 | 84 | | Add(word); |
| 17 | 85 | | } |
| 6 | 86 | | } |
| | 87 | | } |
| | 88 | |
|
| | 89 | | private class TrieNode |
| | 90 | | { |
| 228 | 91 | | public Dictionary<char, TrieNode> Children { get; } = []; |
| | 92 | |
|
| 106 | 93 | | public string? Word { get; set; } |
| | 94 | | } |
| | 95 | | } |