1
class Solution:
2
def reformat(self, s: str) -> str:
3
# Store the alphabets and the numerics from the string in a seperat arrays
4
alpha = []
5
num = []
6
# Initiate a res variable to store the resultant string
7
res = ""
8

9
for i in s:
10
if i.isalpha():
11
alpha.append(i)
12
else:
13
num.append(i)
14

15
# It's not possible to create a permutation if the absolute difference b/w len(alpha) and len(num) > 1.
16
if abs(len(alpha) - len(num)) > 1:
17
return ""
18

19
# Use Zip to create list of tuples.
20
# For ex:- if alpha = ['a','b'] and num = ['1', '2'] then,
21
# zip(alpha, num) = [('a', '1'), ('b', '2')]
22
for ch, n in zip(alpha, num):
23
res += ch + n
24

25
if len(alpha) > len(num):
26
res += alpha[-1]
27
if len(num) > len(alpha):
28
res = num[-1] + res
29

30
return res

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0