Skip to content

Reduced Time Complexity #5236

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions strings/alternative_string_arrange.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,29 @@ def alternative_string_arrange(first_str: str, second_str: str) -> str:
>>> alternative_string_arrange("ABC", "")
'ABC'
"""

# Base Condition
if len(first_str)==0:
return second_str
if len(second_str)==0:
return first_str
Comment on lines +18 to +21
Copy link
Member

@cclauss cclauss Oct 11, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if len(first_str)==0:
return second_str
if len(second_str)==0:
return first_str
if not first_str or not second_str:
return first_str and second_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
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])
return "".join(output_list)
else:
break
return "".join(output_list) + second_str[char_count+1:] + first_str[char_count+1:]


if __name__ == "__main__":
Expand Down