-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge_strings_alternately.py
98 lines (74 loc) · 3.01 KB
/
merge_strings_alternately.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/usr/bin/env python3
# Merge Strings Alternately
#
# https://leetcode.com/problems/merge-strings-alternately
#
# You are given two strings word1 and word2. Merge the strings by adding letters
# in alternating order, starting with word1. If a string is longer than the
# other, append the additional letters onto the end of the merged string.
# Return the merged string.
def test():
"""
Run `pytest <this-file>`.
"""
def test_algo(algo):
assert algo(word1 = "abc", word2 = "pqr") == "apbqcr"
assert algo(word1 = "ab", word2 = "pqrs") == "apbqrs"
assert algo(word1 = "abcd", word2 = "pq") == "apbqcd"
# Test all different algorithms/implementations
solution = Solution()
for algo in [solution.two_pointer, solution.one_pointer, solution.functional]:
test_algo(algo)
class Solution:
def two_pointer(self, word1: str, word2: str) -> str:
"""
Approach: Two pointer.
Idea: First take a char from each word until either is empty, then take rest from non-empty word.
Time: O(n): We iterate over every element of each of the elements.
Space: O(1): We store no additional memory.
Leetcode: 34 ms runtime, 16.54 MB memory
"""
output = ""
word1_idx = 0
word2_idx = 0
while word1_idx < len(word1) and word2_idx < len(word2):
output += word1[word1_idx]
output += word2[word2_idx]
word1_idx += 1
word2_idx += 1
# Consume remaining.
if word1_idx < len(word1):
output += word1[word1_idx:]
elif word2_idx < len(word2):
output += word2[word2_idx:]
return output
def one_pointer(self, word1: str, word2: str) -> str:
"""
Approach: One pointer.
Idea: First take a char from each word until either is empty, then take rest from non-empty word.
Time: O(n): We iterate over every element of each of the elements.
Space: O(1): We store no additional memory.
Leetcode: 24 ms runtime, 16.56 MB memory
"""
output = ""
i = 0
while i < len(word1) and i < len(word2):
output += word1[i]
output += word2[i]
i += 1
# Consume remaining.
if i < len(word1):
output += word1[i:]
elif i < len(word2):
output += word2[i:]
return output
def functional(self, word1: str, word2: str) -> str:
"""
Approach: Functional programming approach.
Idea: Zip words together, and fill in missing chars of shorter word with empty string.
Time: O(n): We iterate over every element of each of the elements.
Space: O(1): We store no additional memory.
Leetcode: 27 ms runtime, 16.54 MB memory
"""
import itertools
return "".join(f"{char1}{char2}" for (char1, char2) in itertools.zip_longest(word1, word2, fillvalue=""))