< Summary

Information
Class: LeetCode.Algorithms.SumOfDigitsOfStringAfterConvert.SumOfDigitsOfStringAfterConvertIterative
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\SumOfDigitsOfStringAfterConvert\SumOfDigitsOfStringAfterConvertIterative.cs
Line coverage
100%
Covered lines: 24
Uncovered lines: 0
Coverable lines: 24
Total lines: 58
Line coverage: 100%
Branch coverage
100%
Covered branches: 8
Total branches: 8
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
GetLucky(...)100%88100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\SumOfDigitsOfStringAfterConvert\SumOfDigitsOfStringAfterConvertIterative.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.SumOfDigitsOfStringAfterConvert;
 13
 14/// <inheritdoc />
 15public class SumOfDigitsOfStringAfterConvertIterative : ISumOfDigitsOfStringAfterConvert
 16{
 17    /// <summary>
 18    ///     Time complexity - O(n)
 19    ///     Space complexity - O(1)
 20    /// </summary>
 21    /// <param name="s"></param>
 22    /// <param name="k"></param>
 23    /// <returns></returns>
 24    public int GetLucky(string s, int k)
 925    {
 926        var sum = 0;
 27
 93128        foreach (var c in s)
 45229        {
 45230            var number = c - 'a' + 1;
 31
 123232            while (number > 0)
 78033            {
 78034                sum += number % 10;
 35
 78036                number /= 10;
 78037            }
 45238        }
 39
 2740        while (k > 1)
 1841        {
 1842            var currentSum = 0;
 43
 5044            while (sum > 0)
 3245            {
 3246                currentSum += sum % 10;
 47
 3248                sum /= 10;
 3249            }
 50
 1851            sum = currentSum;
 52
 1853            k--;
 1854        }
 55
 956        return sum;
 957    }
 58}