aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--total_char_str_transform1.c40
-rw-r--r--total_char_str_transform1.py31
2 files changed, 71 insertions, 0 deletions
diff --git a/total_char_str_transform1.c b/total_char_str_transform1.c
new file mode 100644
index 0000000..50ee3a9
--- /dev/null
+++ b/total_char_str_transform1.c
@@ -0,0 +1,40 @@
+// 3335. Total Characters in String After Transformations I
+#include "leetcode.h"
+
+/*
+ * you are given a string 's' and an integer 't' representing the number of
+ * transformations to perform. in one transformation, every character in 's' is
+ * replaced according to the following rules: if the character is 'z', replace
+ * it with the string 'ab'. otherwise, replace it with the next character in the
+ * alphabet. return the length of the resulting string after exactly 't'
+ * transformations. since the answer may be very large, return it modulo 10^9
+ * + 7.
+ */
+
+int lengthAfterTransformations(char *s, int t) {
+ const int mod = 1e9 + 7;
+ int cnt[26] = {0}, n = strlen(s);
+ for (int i = 0; i < n; i++)
+ cnt[s[i] - 'a']++;
+ for (int i = 0; i < t; i++) {
+ int tmp[26] = {0};
+ for (int j = 0; j < 26; j++) {
+ if (j == 25) {
+ tmp[0] = (tmp[0] + cnt[j]) % mod;
+ tmp[1] = (tmp[1] + cnt[j]) % mod;
+ } else
+ tmp[j + 1] = (tmp[j + 1] + cnt[j]) % mod;
+ }
+ memcpy(cnt, tmp, sizeof(cnt));
+ }
+ int ans = 0;
+ for (int i = 0; i < 26; i++)
+ ans = (ans + cnt[i]) % mod;
+ return ans;
+}
+
+int main() {
+ char *s1 = "abcyy", *s2 = "azbk";
+ printf("%d\n", lengthAfterTransformations(s1, 2)); // expect: 7
+ printf("%d\n", lengthAfterTransformations(s2, 1)); // expect: 5
+}
diff --git a/total_char_str_transform1.py b/total_char_str_transform1.py
new file mode 100644
index 0000000..d4b2471
--- /dev/null
+++ b/total_char_str_transform1.py
@@ -0,0 +1,31 @@
+# 3335. Total Characters in String After Transformations I
+
+"""
+you are given a string 's' and an integer 't' representing the number of
+transformations to perform. in one transformation, every character in 's' is
+replaced according to the following rules: if the character is 'z', replace
+it with the string 'ab'. otherwise, replace it with the next character in the
+alphabet. return the length of the resulting string after exactly 't'
+transformations. since the answer may be very large, return it modulo 10^9
++ 7.
+"""
+
+
+class Solution(object):
+ def lengthAfterTransformations(self, s, t):
+ """
+ :type s: str
+ :type t: int
+ :rtype: int
+ """
+ mod = 10**9 + 7
+ dp = [1] * 26 + [0] * 100100
+ for i in range(26, 100100):
+ dp[i] = (dp[i - 26] + dp[i - 26 + 1]) % mod
+ return sum(dp[ord(c) - ord("a") + t] for c in s) % mod
+
+
+if __name__ == "__main__":
+ obj = Solution()
+ print(obj.lengthAfterTransformations(s="abcyy", t=2))
+ print(obj.lengthAfterTransformations(s="azbk", t=1))