| | 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 | |
|
| | 12 | | using System.Text; |
| | 13 | |
|
| | 14 | | namespace LeetCode.Algorithms.ShiftingLetters2; |
| | 15 | |
|
| | 16 | | /// <inheritdoc /> |
| | 17 | | public 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) |
| 3 | 29 | | { |
| 3 | 30 | | var differenceArray = new int[s.Length]; |
| | 31 | |
|
| 37 | 32 | | foreach (var shift in shifts) |
| 14 | 33 | | { |
| 14 | 34 | | var start = shift[0]; |
| 14 | 35 | | var end = shift[1]; |
| 14 | 36 | | var direction = shift[2]; |
| | 37 | |
|
| 14 | 38 | | if (direction == 0) |
| 7 | 39 | | { |
| 7 | 40 | | differenceArray[start]--; |
| | 41 | |
|
| 7 | 42 | | if (end + 1 < s.Length) |
| 5 | 43 | | { |
| 5 | 44 | | differenceArray[end + 1]++; |
| 5 | 45 | | } |
| 7 | 46 | | } |
| | 47 | | else |
| 7 | 48 | | { |
| 7 | 49 | | differenceArray[start]++; |
| | 50 | |
|
| 7 | 51 | | if (end + 1 < s.Length) |
| 5 | 52 | | { |
| 5 | 53 | | differenceArray[end + 1]--; |
| 5 | 54 | | } |
| 7 | 55 | | } |
| 14 | 56 | | } |
| | 57 | |
|
| 3 | 58 | | var numberOfShifts = 0; |
| | 59 | |
|
| 3 | 60 | | var result = new StringBuilder(s); |
| | 61 | |
|
| 38 | 62 | | for (var i = 0; i < differenceArray.Length; i++) |
| 16 | 63 | | { |
| 16 | 64 | | numberOfShifts = (numberOfShifts + differenceArray[i]) % LettersCount; |
| | 65 | |
|
| 16 | 66 | | if (numberOfShifts < 0) |
| 3 | 67 | | { |
| 3 | 68 | | numberOfShifts += LettersCount; |
| 3 | 69 | | } |
| | 70 | |
|
| 16 | 71 | | result[i] = (char)('a' + ((s[i] - 'a' + numberOfShifts) % LettersCount)); |
| 16 | 72 | | } |
| | 73 | |
|
| 3 | 74 | | return result.ToString(); |
| 3 | 75 | | } |
| | 76 | | } |