aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJack Sangdahl <[email protected]>2024-09-02 18:33:53 -0600
committerJack Sangdahl <[email protected]>2024-09-02 18:33:53 -0600
commit8f4686e3871ac66ce00447f429cff45c8032ffa4 (patch)
treea14c7d7c58db8d9f4902806d90563e0965359105
parentf24c80dde79ceb46dda91c612a91f0758e94b5b7 (diff)
downloadleetcode-8f4686e3871ac66ce00447f429cff45c8032ffa4.tar.gz
leetcode-8f4686e3871ac66ce00447f429cff45c8032ffa4.zip
1945. sum of digits of string after convert
-rw-r--r--sum_digit_str_convert.c51
-rw-r--r--sum_digit_str_convert.py30
2 files changed, 81 insertions, 0 deletions
diff --git a/sum_digit_str_convert.c b/sum_digit_str_convert.c
new file mode 100644
index 0000000..d3a74be
--- /dev/null
+++ b/sum_digit_str_convert.c
@@ -0,0 +1,51 @@
+// 1945. Sum of Digits of String After Convert
+#include "leetcode.h"
+
+/*
+ * you are given a string 's' consisting of lowercase english letters, and an
+ * integer 'k'. first, convert 's' into an integer by replacing each letter with
+ * its position in the alphabet (ie. replace 'a' with 1, 'b' with 2, and so on).
+ * then, transform the integer by replacing it with the sum of its digits.
+ * repeat the transform operation 'k' times in total. return the resulting
+ * integer after performing the operations described above
+ */
+
+int transform(int sum, int k) {
+ int res;
+ for (int i = 1; i < k; i++) {
+ res = 0;
+ while (sum) {
+ int m = sum % 10;
+ res += m;
+ sum /= 10;
+ }
+ sum = res;
+ }
+ return res;
+}
+
+int getLucky(char *s, int k) {
+ int n = strlen(s), arr[n], ans = 0, pos = 0, sum = 0;
+ for (int i = 0; i < n; i++) {
+ char curr = s[i];
+ pos = curr - 96;
+ arr[i] = pos;
+ }
+ for (int i = 0; i < n; i++) {
+ if (arr[i] <= 9)
+ sum += arr[i];
+ else {
+ int x = arr[i] % 10, y = arr[i] / 10;
+ sum += (x + y);
+ }
+ }
+ ans = k == 1 ? sum : transform(sum, k);
+ return ans;
+}
+
+int main() {
+ char *s1 = "iiii", *s2 = "leetcode", *s3 = "zbax";
+ printf("%d\n", getLucky(s1, 1)); // expect: 36
+ printf("%d\n", getLucky(s2, 2)); // expect: 6
+ printf("%d\n", getLucky(s3, 2)); // expect: 8
+}
diff --git a/sum_digit_str_convert.py b/sum_digit_str_convert.py
new file mode 100644
index 0000000..3980972
--- /dev/null
+++ b/sum_digit_str_convert.py
@@ -0,0 +1,30 @@
+# 1945. Sum of Digits of String After Convert
+
+"""
+you are given a string 's' consisting of lowercase english letters, and an
+integer 'k'. first, convert 's' into an integer by replacing each letter with
+its position in the alphabet (ie. replace 'a' with 1, 'b' with 2, and so on).
+then, transform the integer by replacing it with the sum of its digits.
+repeat the transform operation 'k' times in total. return the resulting
+integer after performing the operations described above
+"""
+
+
+class Solution(object):
+ def getLucky(self, s, k):
+ """
+ :type s: str
+ :type k: int
+ :rtype: int
+ """
+ s = "".join(str(ord(i) - ord("a") + 1) for i in s)
+ for _ in range(k):
+ s = str(sum(int(i) for i in s))
+ return int(s)
+
+
+if __name__ == "__main__":
+ obj = Solution()
+ print(obj.getLucky(s="iiii", k=1))
+ print(obj.getLucky(s="leetcode", k=2))
+ print(obj.getLucky(s="zbax", k=2))