Skip to content

Commit cf156b7

Browse files
add alternative_string_arrange method
1 parent 5957eab commit cf156b7

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

strings/alternative_string_arrange.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
def alternative_string_arrange(first_str: str, second_str: str) -> str:
2+
"""
3+
Return the alternative arrangements of the two strings.
4+
:param first_str:
5+
:param second_str:
6+
:return: String
7+
>>> alternative_string_arrange("ABCD", "XY")
8+
AXBYCD
9+
>>> alternative_string_arrange("XY", "ABCD")
10+
XAYBCD
11+
>>> alternative_string_arrange("AB", "XYZ")
12+
AXBYZ
13+
>>> alternative_string_arrange("ABC", "")
14+
ABC
15+
"""
16+
first_str_length: int = len(first_str)
17+
second_str_length: int = len(second_str)
18+
abs_length: int = (
19+
first_str_length if first_str_length > second_str_length else second_str_length
20+
)
21+
li: list = []
22+
for char_count in range(abs_length):
23+
if char_count < first_str_length:
24+
li.append(first_str[char_count])
25+
if char_count < second_str_length:
26+
li.append(second_str[char_count])
27+
return "".join(li)
28+
29+
30+
if __name__ == "__main__":
31+
print(alternative_string_arrange("AB", "XYZ"), end=" ")

0 commit comments

Comments
 (0)