-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleetcode848
More file actions
54 lines (46 loc) · 1.5 KB
/
leetcode848
File metadata and controls
54 lines (46 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
848. 字母移位
题目难度 Medium
有一个由小写字母组成的字符串 S,和一个整数数组 shifts。
我们将字母表中的下一个字母称为原字母的 移位(由于字母表是环绕的, 'z' 将会变成 'a')。
例如·,shift('a') = 'b', shift('t') = 'u',, 以及 shift('z') = 'a'。
对于每个 shifts[i] = x , 我们会将 S 中的前 i+1 个字母移位 x 次。
返回将所有这些移位都应用到 S 后最终得到的字符串。
示例:
输入:S = "abc", shifts = [3,5,9]
输出:"rpl"
解释:
我们以 "abc" 开始。
将 S 中的第 1 个字母移位 3 次后,我们得到 "dbc"。
再将 S 中的前 2 个字母移位 5 次后,我们得到 "igc"。
最后将 S 中的这 3 个字母移位 9 次后,我们得到答案 "rpl"。
提示:
1 <= S.length = shifts.length <= 20000
0 <= shifts[i] <= 10 ^ 9
letter=' '.join(map(chr,range(97,123))).split()
letter_2_idx={}
idx_2_letter={}
for i,l in enumerate(letter):
letter_2_idx[l]=i
idx_2_letter[i]=l
def switch_str(S,shifts):
# new_S=''
S_n=[]
for si in S:
S_n.append(letter_2_idx[si])
S=S_n
for idx_len,shif in enumerate(shifts):
idx_len=idx_len+1
new_S = []
for i,v in enumerate(S):
if idx_len>=i+1:
new_S.append(v+shif)
else:
new_S.append(v)
S=new_S
res=''
for i in S:
if i>26:
i=i%26
res+=idx_2_letter[i]
return res
print(switch_str('abc',[3,5,9]))