-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReformatTheString.py
32 lines (28 loc) · 1001 Bytes
/
ReformatTheString.py
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
class Solution:
def reformat(self, s: str) -> str:
letter = []
digit = []
length_letter = 0
length_digit = 0
for i in s:
if 'a' <= i <= 'z':
letter.append(i)
length_letter += 1
else:
digit.append(i)
length_digit += 1
output = []
if abs(length_letter-length_digit) >= 2:
return ""
elif length_letter > length_digit:
for i in range(length_digit):
output.extend([letter[i], digit[i]])
output.append(letter[length_letter-1])
elif length_digit > length_letter:
for i in range(length_letter):
output.extend([digit[i], letter[i]])
output.append(digit[length_digit-1])
else:
for i in range(length_digit):
output.extend([letter[i], digit[i]])
return ''.join(output)