< Summary

Information
Class: LeetCode.Algorithms.MinimumASCIIDeleteSumForTwoStrings.MinimumASCIIDeleteSumForTwoStringsDynamicProgramming
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\MinimumASCIIDeleteSumForTwoStrings\MinimumASCIIDeleteSumForTwoStringsDynamicProgramming.cs
Line coverage
91%
Covered lines: 31
Uncovered lines: 3
Coverable lines: 34
Total lines: 68
Line coverage: 91.1%
Branch coverage
92%
Covered branches: 13
Total branches: 14
Branch coverage: 92.8%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
MinimumDeleteSum(...)92.85%141491.17%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\MinimumASCIIDeleteSumForTwoStrings\MinimumASCIIDeleteSumForTwoStringsDynamicProgramming.cs

#LineLine coverage
 1// --------------------------------------------------------------------------------
 2// Copyright (C) 2026 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.MinimumASCIIDeleteSumForTwoStrings;
 13
 14/// <inheritdoc />
 15public sealed class MinimumASCIIDeleteSumForTwoStringsDynamicProgramming : IMinimumASCIIDeleteSumForTwoStrings
 16{
 17    /// <summary>
 18    ///     Time complexity - O(n * m)
 19    ///     Space complexity - O(min(n, m))
 20    /// </summary>
 21    /// <param name="s1"></param>
 22    /// <param name="s2"></param>
 23    /// <returns></returns>
 24    public int MinimumDeleteSum(string s1, string s2)
 225    {
 226        if (s1.Length < s2.Length)
 027        {
 028            (s1, s2) = (s2, s1);
 029        }
 30
 231        Span<int> dp = stackalloc int[s2.Length + 1];
 32
 2233        for (var i = 0; i < s1.Length; i++)
 934        {
 935            var previous = 0;
 36
 8437            for (var j = 0; j < s2.Length; j++)
 3338            {
 3339                var current = dp[j + 1];
 40
 3341                if (s1[i] == s2[j])
 1042                {
 1043                    dp[j + 1] = previous + s1[i];
 1044                }
 2345                else if (dp[j + 1] <= dp[j])
 1746                {
 1747                    dp[j + 1] = dp[j];
 1748                }
 49
 3350                previous = current;
 3351            }
 952        }
 53
 254        var totalAscii = 0;
 55
 2456        foreach (var c in s1)
 957        {
 958            totalAscii += c;
 959        }
 60
 2061        foreach (var c in s2)
 762        {
 763            totalAscii += c;
 764        }
 65
 266        return totalAscii - (2 * dp[^1]);
 267    }
 68}