< Summary

Information
Class: LeetCode.Algorithms.ApplyDiscountToPrices.ApplyDiscountToPricesIterative
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\ApplyDiscountToPrices\ApplyDiscountToPricesIterative.cs
Line coverage
100%
Covered lines: 27
Uncovered lines: 0
Coverable lines: 27
Total lines: 62
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
DiscountPrices(...)100%1414100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\ApplyDiscountToPrices\ApplyDiscountToPricesIterative.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
 12using System.Text;
 13
 14namespace LeetCode.Algorithms.ApplyDiscountToPrices;
 15
 16/// <inheritdoc />
 17public class ApplyDiscountToPricesIterative : IApplyDiscountToPrices
 18{
 19    /// <summary>
 20    ///     Time complexity - O(n)
 21    ///     Space complexity - O(n)
 22    /// </summary>
 23    /// <param name="sentence"></param>
 24    /// <param name="discount"></param>
 25    /// <returns></returns>
 26    public string DiscountPrices(string sentence, int discount)
 327    {
 328        var resultStringBuilder = new StringBuilder(sentence.Length);
 29
 330        var words = sentence.Split(' ');
 31
 4832        for (var i = 0; i < words.Length; i++)
 2133        {
 2134            if (words[i].StartsWith('$') && words[i].Length > 1 && double.TryParse(words[i].AsSpan(1), out var number))
 735            {
 736                var pricePart = words[i].AsSpan(1);
 37
 738                if (pricePart.Length > 1 && pricePart[1] == 'e')
 139                {
 140                    resultStringBuilder.Append(words[i]);
 141                }
 42                else
 643                {
 644                    var discountedPrice = number - (number * discount / 100);
 45
 646                    resultStringBuilder.Append($"${discountedPrice:F2}");
 647                }
 748            }
 49            else
 1450            {
 1451                resultStringBuilder.Append(words[i]);
 1452            }
 53
 2154            if (i < words.Length - 1)
 1855            {
 1856                resultStringBuilder.Append(' ');
 1857            }
 2158        }
 59
 360        return resultStringBuilder.ToString();
 361    }
 62}