Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 111b570

Browse files
authored
Create Total Characters in String After Transformations I.py
1 parent bfea5b0 commit 111b570

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /