< Summary

Information
Class: LeetCode.Algorithms.FreedomTrail.FreedomTrailDynamicProgramming
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\FreedomTrail\FreedomTrailDynamicProgramming.cs
Line coverage
100%
Covered lines: 38
Uncovered lines: 0
Coverable lines: 38
Total lines: 74
Line coverage: 100%
Branch coverage
100%
Covered branches: 18
Total branches: 18
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
FindRotateSteps(...)100%1818100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\FreedomTrail\FreedomTrailDynamicProgramming.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.FreedomTrail;
 13
 14/// <inheritdoc />
 15public class FreedomTrailDynamicProgramming : IFreedomTrail
 16{
 17    /// <summary>
 18    ///     Time complexity - O(m * n^2)
 19    ///     Space complexity - O(m * n)
 20    /// </summary>
 21    /// <param name="ring"></param>
 22    /// <param name="key"></param>
 23    /// <returns></returns>
 24    public int FindRotateSteps(string ring, string key)
 625    {
 1226        int n = ring.Length, m = key.Length;
 27
 628        var dp = new int[m + 1, n];
 29
 15830        for (var i = 0; i <= m; i++)
 7331        {
 184032            for (var j = 0; j < n; j++)
 84733            {
 84734                dp[i, j] = int.MaxValue;
 84735            }
 7336        }
 37
 638        dp[0, 0] = 0;
 39
 14640        for (var i = 1; i <= m; i++)
 6741        {
 174042            for (var j = 0; j < n; j++)
 80343            {
 80344                if (ring[j] != key[i - 1])
 66845                {
 66846                    continue;
 47                }
 48
 378049                for (var k = 0; k < n; k++)
 175550                {
 175551                    if (dp[i - 1, k] == int.MaxValue)
 147352                    {
 147353                        continue;
 54                    }
 55
 28256                    var steps = Math.Min((j - k + n) % n, (k - j + n) % n);
 28257                    dp[i, j] = Math.Min(dp[i, j], dp[i - 1, k] + steps + 1);
 28258                }
 13559            }
 6760        }
 61
 662        var minSteps = int.MaxValue;
 63
 10064        for (var j = 0; j < n; j++)
 4465        {
 4466            if (ring[j] == key[m - 1])
 867            {
 868                minSteps = Math.Min(minSteps, dp[m, j]);
 869            }
 4470        }
 71
 672        return minSteps;
 673    }
 74}