aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJack Sangdahl <[email protected]>2025-01-05 09:35:35 +0100
committerJack Sangdahl <[email protected]>2025-01-05 09:35:35 +0100
commit441ce5ccc60300de3e0380771f403bd37b2fc611 (patch)
tree2018f3ed7b82e7bf9084a6094e94ba99001ce15f
parent2a8a768249be900c9946204b45519ebacbb9b485 (diff)
downloadleetcode-441ce5ccc60300de3e0380771f403bd37b2fc611.tar.gz
leetcode-441ce5ccc60300de3e0380771f403bd37b2fc611.zip
2381. shifting letters 2
-rw-r--r--shifting_letters2.c48
-rw-r--r--shifting_letters2.py40
2 files changed, 88 insertions, 0 deletions
diff --git a/shifting_letters2.c b/shifting_letters2.c
new file mode 100644
index 0000000..de20259
--- /dev/null
+++ b/shifting_letters2.c
@@ -0,0 +1,48 @@
+// 2381. Shifting Letters II
+#include "leetcode.h"
+
+/*
+ * you are given a string 's' of lowercase english letters and a 2d integer
+ * array 'shifts' where 'shifts[i] = [start_i, end_i, direction_i]'. for every
+ * 'i', shift the characters in 's' from the index 'start_i' to he index 'end_i'
+ * (inclusive) forward if the 'direction_i = 1' or shift the characters backward
+ * if 'direction_i = 0'. shifting a character forward means replacing it with
+ * the next letter in the alphabet. similarily, shifting a character backward
+ * means replacing it with the previous alphabetic character. return the final
+ * string ater all such shifts to 's' are applied
+ */
+
+char *shiftingLetters(char *s, int **shifts, int shiftsSize,
+ int *shiftsColSize) {
+ int n = strlen(s), *shift = (int *)calloc(n + 1, sizeof(int));
+ for (int i = 0; i < shiftsSize; ++i) {
+ int start = shifts[i][0], end = shifts[i][1], direction = shifts[i][2];
+ shift[start] += (direction == 1 ? 1 : -1);
+ if (end + 1 < n)
+ shift[end + 1] -= (direction == 1 ? 1 : -1);
+ }
+ int curr = 0;
+ for (int i = 0; i < n; ++i) {
+ curr += shift[i];
+ shift[i] = curr;
+ }
+ for (int i = 0; i < n; ++i) {
+ int net = (shift[i] % 26 + 26) % 26;
+ s[i] = 'a' + (s[i] - 'a' + net) % 26;
+ }
+ free(shift);
+ return s;
+}
+
+int main() {
+ char *s1 = "abc", *s2 = "dztz";
+ int s1i[3][3] = {{0, 1, 0}, {1, 2, 1}, {0, 2, 1}},
+ s2i[2][3] = {{0, 0, 0}, {1, 1, 1}};
+ struct two_d_arr sh1, sh2;
+ two_d_arr_init(&sh1, 3, 3, s1i);
+ two_d_arr_init(&sh1, 2, 3, s2i);
+ printf("%s\n", shiftingLetters(s1, sh1.arr, sh1.row_size,
+ sh1.col_size)); // expect: ace
+ printf("%s\n", shiftingLetters(s2, sh2.arr, sh2.row_size,
+ sh2.col_size)); // expect: catz
+}
diff --git a/shifting_letters2.py b/shifting_letters2.py
new file mode 100644
index 0000000..83058fc
--- /dev/null
+++ b/shifting_letters2.py
@@ -0,0 +1,40 @@
+# 2381. Shifting Letters II
+
+"""
+you are given a string 's' of lowercase english letters and a 2d integer
+array 'shifts' where 'shifts[i] = [start_i, end_i, direction_i]'. for every
+'i', shift the characters in 's' from the index 'start_i' to he index 'end_i'
+(inclusive) forward if the 'direction_i = 1' or shift the characters backward
+if 'direction_i = 0'. shifting a character forward means replacing it with
+the next letter in the alphabet. similarily, shifting a character backward
+means replacing it with the previous alphabetic character. return the final
+string ater all such shifts to 's' are applied
+"""
+
+
+class Solution(object):
+ def shiftingLetters(self, s, shifts):
+ """
+ :type s: str
+ :type shifts: List[List[int]]
+ :rtype: str
+ """
+ n = len(s)
+ shift = [0] * (n + 1)
+ for i in shifts:
+ start, end, direction = i
+ shift[start] += 1 if direction == 1 else -1
+ if end + 1 < n:
+ shift[end + 1] -= 1 if direction == 1 else -1
+ curr, ans = 0, list(s)
+ for i in range(n):
+ curr += shift[i]
+ net = (curr % 26 + 26) % 26
+ ans[i] = chr((ord(ans[i]) - ord("a") + net) % 26 + ord("a"))
+ return "".join(ans)
+
+
+if __name__ == "__main__":
+ obj = Solution()
+ print(obj.shiftingLetters(s="abc", shifts=[[0, 1, 0], [1, 2, 1], [0, 2, 1]]))
+ print(obj.shiftingLetters(s="dztz", shifts=[[0, 0, 0], [1, 1, 1]]))