|
| 1 | +''' |
| 2 | +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: |
| 3 | + |
| 4 | +If the character is 'z', replace it with the string "ab". |
| 5 | +Otherwise, replace it with the next character in the alphabet. For example, 'a' is replaced with 'b', 'b' is replaced with 'c', and so on. |
| 6 | +Return the length of the resulting string after exactly t transformations. |
| 7 | + |
| 8 | +Since the answer may be very large, return it modulo 109 + 7. |
| 9 | +''' |
| 10 | + |
| 11 | +class Solution: |
| 12 | + def lengthAfterTransformations(self, s: str, t: int) -> int: |
| 13 | + |
| 14 | + m = 10**9+7 |
| 15 | + n = len(s) |
| 16 | + |
| 17 | + d = dict() |
| 18 | + d['z'] = 'ab' |
| 19 | + res = '' |
| 20 | + for i in range(t): |
| 21 | + if s[i]!= 'z': |
| 22 | + res += chr(ord(s[i]) + 1) |
| 23 | + else: |
| 24 | + res += 'ab' |
| 25 | + return len(res) |
| 26 | + |
| 27 | + |
| 28 | +----------------------------------------------------------------------------------------- |
| 29 | +class Solution: |
| 30 | + def lengthAfterTransformations(self, s: str, t: int) -> int: |
| 31 | + MOD = 10**9 + 7 |
| 32 | + cnt = [0] * 26 |
| 33 | + res = len(s) |
| 34 | + z = 25 |
| 35 | + |
| 36 | + for c in s: |
| 37 | + cnt[ord(c) - ord('a')] += 1 |
| 38 | + |
| 39 | + for _ in range(t): |
| 40 | + res = (res + cnt[z]) % MOD |
| 41 | + cnt[(z + 1) % 26] = (cnt[(z + 1) % 26] + cnt[z]) % MOD |
| 42 | + z = (z + 25) % 26 |
| 43 | + |
| 44 | + return res |
0 commit comments