| | 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.ExtraCharactersInString; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public class ExtraCharactersInStringDynamicProgrammingTrie : IExtraCharactersInString |
| | 16 | | { |
| | 17 | | /// <summary> |
| | 18 | | /// Time complexity - O(L^2 × n), where L is the length of s and n is the length of the longest word in the dict |
| | 19 | | /// Space complexity - O(m * n + L), where L is the length of s, m is the number of words in the dictionary and |
| | 20 | | /// the length of the longest word in the dictionary |
| | 21 | | /// </summary> |
| | 22 | | /// <param name="s"></param> |
| | 23 | | /// <param name="dictionary"></param> |
| | 24 | | /// <returns></returns> |
| | 25 | | public int MinExtraChar(string s, string[] dictionary) |
| 6 | 26 | | { |
| 6 | 27 | | var trie = new Trie(dictionary); |
| | 28 | |
|
| 6 | 29 | | var dp = new int[s.Length + 1]; |
| | 30 | |
|
| 226 | 31 | | for (var start = s.Length - 1; start >= 0; start--) |
| 107 | 32 | | { |
| 107 | 33 | | dp[start] = dp[start + 1] + 1; |
| | 34 | |
|
| 107 | 35 | | var node = trie.Root; |
| | 36 | |
|
| 444 | 37 | | for (var end = start; end < s.Length; end++) |
| 218 | 38 | | { |
| 218 | 39 | | if (!node.Children.ContainsKey(s[end])) |
| 103 | 40 | | { |
| 103 | 41 | | break; |
| | 42 | | } |
| | 43 | |
|
| 115 | 44 | | node = node.Children[s[end]]; |
| | 45 | |
|
| 115 | 46 | | if (node.Word != null) |
| 30 | 47 | | { |
| 30 | 48 | | dp[start] = Math.Min(dp[start], dp[end + 1]); |
| 30 | 49 | | } |
| 115 | 50 | | } |
| 107 | 51 | | } |
| | 52 | |
|
| 6 | 53 | | return dp[0]; |
| 6 | 54 | | } |
| | 55 | |
|
| | 56 | | private class Trie |
| | 57 | | { |
| 6 | 58 | | public Trie(IEnumerable<string> words) |
| 6 | 59 | | { |
| 6 | 60 | | AddRange(words); |
| 6 | 61 | | } |
| | 62 | |
|
| 159 | 63 | | public TrieNode Root { get; } = new(); |
| | 64 | |
|
| | 65 | | private void Add(string word) |
| 46 | 66 | | { |
| 46 | 67 | | var node = Root; |
| | 68 | |
|
| 440 | 69 | | foreach (var c in word) |
| 151 | 70 | | { |
| 151 | 71 | | if (!node.Children.TryGetValue(c, out var value)) |
| 140 | 72 | | { |
| 140 | 73 | | value = new TrieNode(); |
| | 74 | |
|
| 140 | 75 | | node.Children[c] = value; |
| 140 | 76 | | } |
| | 77 | |
|
| 151 | 78 | | node = value; |
| 151 | 79 | | } |
| | 80 | |
|
| 46 | 81 | | node.Word = word; |
| 46 | 82 | | } |
| | 83 | |
|
| | 84 | | private void AddRange(IEnumerable<string> words) |
| 6 | 85 | | { |
| 110 | 86 | | foreach (var word in words) |
| 46 | 87 | | { |
| 46 | 88 | | Add(word); |
| 46 | 89 | | } |
| 6 | 90 | | } |
| | 91 | | } |
| | 92 | |
|
| | 93 | | private class TrieNode |
| | 94 | | { |
| 770 | 95 | | public Dictionary<char, TrieNode> Children { get; } = []; |
| | 96 | |
|
| 161 | 97 | | public string? Word { get; set; } |
| | 98 | | } |
| | 99 | | } |