< Summary

Information
Class: LeetCode.Algorithms.ExtraCharactersInString.ExtraCharactersInStringDynamicProgrammingHashSet
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\ExtraCharactersInString\ExtraCharactersInStringDynamicProgrammingHashSet.cs
Line coverage
100%
Covered lines: 19
Uncovered lines: 0
Coverable lines: 19
Total lines: 52
Line coverage: 100%
Branch coverage
100%
Covered branches: 6
Total branches: 6
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%66100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\ExtraCharactersInString\ExtraCharactersInStringDynamicProgrammingHashSet.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 ExtraCharactersInStringDynamicProgrammingHashSet : IExtraCharactersInString
 16{
 17    /// <summary>
 18    ///     Time complexity - O(n^3), n is the length of the longest word in the dictionary
 19    ///     Space complexity - O(n + m), m is the number of words in the dictionary and n is the length of the longest w
 20    ///     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 dp = new int[s.Length + 1];
 28
 629        Array.Fill(dp, int.MaxValue);
 30
 631        dp[0] = 0;
 32
 633        var wordsHashSet = new HashSet<string>(dictionary);
 34
 22635        for (var i = 1; i < dp.Length; i++)
 10736        {
 10737            dp[i] = dp[i - 1] + 1;
 38
 329639            for (var j = 0; j < i; j++)
 154140            {
 154141                var word = s.Substring(j, i - j);
 42
 154143                if (wordsHashSet.Contains(word))
 3044                {
 3045                    dp[i] = Math.Min(dp[i], dp[j]);
 3046                }
 154147            }
 10748        }
 49
 650        return dp[^1];
 651    }
 52}