-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1768.merge-strings-alternately.py
More file actions
46 lines (37 loc) · 1.05 KB
/
1768.merge-strings-alternately.py
File metadata and controls
46 lines (37 loc) · 1.05 KB
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
import unittest
#
# @lc app=leetcode id=1768 lang=python3
#
# [1768] Merge Strings Alternately
#
# @lc code=start
class Solution:
def mergeAlternately(self, word1: str, word2: str) -> str:
# final = []
# len1 = len(word1)
# len2 = len(word2)
# i = 0
# j = 0
# while ((i if len1 > len2 else j) < (len1 if len1 > len2 else len2)):
# if i < len1:
# final.append(word1[i])
# i = i + 1
# if j < len2:
# final.append(word2[j])
# j = j + 1
# return ''.join(final)
final = ''
n = min(len(word1),len(word2))
for i in range(n):
final += word1[i]
final += word2[i]
final += word1[n:]
final += word2[n:]
return final
# @lc code=end
class TestRoot(unittest.TestCase):
def test(self):
sol = Solution()
self.assertEqual(sol.mergeAlternately('abc', 'pqrst'), 'apbqcrst')
if __name__ == '__main__':
unittest.main()