< Summary

Information
Class: LeetCode.Algorithms.ExtraCharactersInString.ExtraCharactersInStringDynamicProgrammingTrie
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\ExtraCharactersInString\ExtraCharactersInStringDynamicProgrammingTrie.cs
Line coverage
100%
Covered lines: 47
Uncovered lines: 0
Coverable lines: 47
Total lines: 99
Line coverage: 100%
Branch coverage
100%
Covered branches: 14
Total branches: 14
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
MinExtraChar(...)100%88100%
.ctor(...)100%11100%
get_Root()100%11100%
Add(...)100%44100%
AddRange(...)100%22100%
get_Children()100%11100%
get_Word()100%11100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\ExtraCharactersInString\ExtraCharactersInStringDynamicProgrammingTrie.cs

#LineLine coverage
 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
 12namespace LeetCode.Algorithms.ExtraCharactersInString;
 13
 14/// <inheritdoc />
 15public 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)
 626    {
 627        var trie = new Trie(dictionary);
 28
 629        var dp = new int[s.Length + 1];
 30
 22631        for (var start = s.Length - 1; start >= 0; start--)
 10732        {
 10733            dp[start] = dp[start + 1] + 1;
 34
 10735            var node = trie.Root;
 36
 44437            for (var end = start; end < s.Length; end++)
 21838            {
 21839                if (!node.Children.ContainsKey(s[end]))
 10340                {
 10341                    break;
 42                }
 43
 11544                node = node.Children[s[end]];
 45
 11546                if (node.Word != null)
 3047                {
 3048                    dp[start] = Math.Min(dp[start], dp[end + 1]);
 3049                }
 11550            }
 10751        }
 52
 653        return dp[0];
 654    }
 55
 56    private class Trie
 57    {
 658        public Trie(IEnumerable<string> words)
 659        {
 660            AddRange(words);
 661        }
 62
 15963        public TrieNode Root { get; } = new();
 64
 65        private void Add(string word)
 4666        {
 4667            var node = Root;
 68
 44069            foreach (var c in word)
 15170            {
 15171                if (!node.Children.TryGetValue(c, out var value))
 14072                {
 14073                    value = new TrieNode();
 74
 14075                    node.Children[c] = value;
 14076                }
 77
 15178                node = value;
 15179            }
 80
 4681            node.Word = word;
 4682        }
 83
 84        private void AddRange(IEnumerable<string> words)
 685        {
 11086            foreach (var word in words)
 4687            {
 4688                Add(word);
 4689            }
 690        }
 91    }
 92
 93    private class TrieNode
 94    {
 77095        public Dictionary<char, TrieNode> Children { get; } = [];
 96
 16197        public string? Word { get; set; }
 98    }
 99}