-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
/
Copy pathalternative_string_arrange.py
42 lines (38 loc) · 1.29 KB
/
alternative_string_arrange.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
33
34
35
36
37
38
39
40
41
42
def alternative_string_arrange(first_str: str, second_str: str) -> str:
"""
Return the alternative arrangements of the two strings.
:param first_str:
:param second_str:
:return: String
>>> alternative_string_arrange("ABCD", "XY")
'AXBYCD'
>>> alternative_string_arrange("XY", "ABCD")
'XAYBCD'
>>> alternative_string_arrange("AB", "XYZ")
'AXBYZ'
>>> alternative_string_arrange("ABC", "")
'ABC'
"""
# Base Condition
if len(first_str)==0:
return second_str
if len(second_str)==0:
return first_str
first_str_length: int = len(first_str)
second_str_length: int = len(second_str)
abs_length: int = (
first_str_length if first_str_length < second_str_length else second_str_length # Take length of Minimum length
)
output_list: list = []
for char_count in range(abs_length):
if char_count < first_str_length:
output_list.append(first_str[char_count])
else:
break
if char_count < second_str_length:
output_list.append(second_str[char_count])
else:
break
return "".join(output_list) + second_str[char_count+1:] + first_str[char_count+1:]
if __name__ == "__main__":
print(alternative_string_arrange("AB", "XYZ"), end=" ")