1417. Reformat The String
Easy
😇 Solution
class Solution:
def alternate(a,b):
ans = ''
ans += a.pop(0)
while(b):
ans += b.pop(0) + a.pop(0)
return ans
def reformat(self, s: str) -> str:
alpha = []
num = []
for c in s:
if c.isalpha():
alpha.append(c)
if c.isnumeric():
num.append(c)
ans = ''
if len(alpha) - len(num) == 1:
ans += Solution.alternate(alpha,num)
elif len(num) - len(alpha) == 1:
ans += Solution.alternate(num,alpha)
elif len(alpha) == len(num):
while(alpha):
ans += alpha.pop(0) + num.pop(0)
return ansLast updated