< Summary

Information
Class: LeetCode.Algorithms.ShiftingLetters2.ShiftingLetters2DifferenceArray
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\ShiftingLetters2\ShiftingLetters2DifferenceArray.cs
Line coverage
100%
Covered lines: 36
Uncovered lines: 0
Coverable lines: 36
Total lines: 76
Line coverage: 100%
Branch coverage
100%
Covered branches: 12
Total branches: 12
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
ShiftingLetters(...)100%1212100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\ShiftingLetters2\ShiftingLetters2DifferenceArray.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.ShiftingLetters2;
 15
 16/// <inheritdoc />
 17public class ShiftingLetters2DifferenceArray : IShiftingLetters2
 18{
 19    private const int LettersCount = 'z' - 'a' + 1;
 20
 21    /// <summary>
 22    ///     Time complexity - O(n + m)
 23    ///     Space complexity - O(n)
 24    /// </summary>
 25    /// <param name="s"></param>
 26    /// <param name="shifts"></param>
 27    /// <returns></returns>
 28    public string ShiftingLetters(string s, int[][] shifts)
 329    {
 330        var differenceArray = new int[s.Length];
 31
 3732        foreach (var shift in shifts)
 1433        {
 1434            var start = shift[0];
 1435            var end = shift[1];
 1436            var direction = shift[2];
 37
 1438            if (direction == 0)
 739            {
 740                differenceArray[start]--;
 41
 742                if (end + 1 < s.Length)
 543                {
 544                    differenceArray[end + 1]++;
 545                }
 746            }
 47            else
 748            {
 749                differenceArray[start]++;
 50
 751                if (end + 1 < s.Length)
 552                {
 553                    differenceArray[end + 1]--;
 554                }
 755            }
 1456        }
 57
 358        var numberOfShifts = 0;
 59
 360        var result = new StringBuilder(s);
 61
 3862        for (var i = 0; i < differenceArray.Length; i++)
 1663        {
 1664            numberOfShifts = (numberOfShifts + differenceArray[i]) % LettersCount;
 65
 1666            if (numberOfShifts < 0)
 367            {
 368                numberOfShifts += LettersCount;
 369            }
 70
 1671            result[i] = (char)('a' + ((s[i] - 'a' + numberOfShifts) % LettersCount));
 1672        }
 73
 374        return result.ToString();
 375    }
 76}