Skip to content

Commit 894303a

Browse files
committed
feat: add solutions to lc problem: No.1768
No.1768.Merge Strings Alternately
1 parent 5e90473 commit 894303a

File tree

6 files changed

+82
-66
lines changed

6 files changed

+82
-66
lines changed

solution/1700-1799/1768.Merge Strings Alternately/README.md

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ word2: p q
5858

5959
<!-- 这里可写通用的实现逻辑 -->
6060

61+
**方法一:直接模拟**
62+
63+
遍历 `word1`, `word2` 两个字符串,依次取出字符,拼接到结果字符串中。Python 代码可以简化为一行。
64+
65+
时间复杂度 $O(m + n)$,忽略答案的空间消耗,空间复杂度 $O(1)$。其中 $m$ 和 $n$ 分别是两个字符串的长度。
66+
6167
<!-- tabs:start -->
6268

6369
### **Python3**
@@ -67,15 +73,7 @@ word2: p q
6773
```python
6874
class Solution:
6975
def mergeAlternately(self, word1: str, word2: str) -> str:
70-
i, m, n = 0, len(word1), len(word2)
71-
res = []
72-
while i < m or i < n:
73-
if i < m:
74-
res.append(word1[i])
75-
if i < n:
76-
res.append(word2[i])
77-
i += 1
78-
return ''.join(res)
76+
return ''.join(a + b for a, b in zip_longest(word1, word2, fillvalue=''))
7977
```
8078

8179
### **Java**
@@ -86,16 +84,16 @@ class Solution:
8684
class Solution {
8785
public String mergeAlternately(String word1, String word2) {
8886
int m = word1.length(), n = word2.length();
89-
StringBuilder res = new StringBuilder();
87+
StringBuilder ans = new StringBuilder();
9088
for (int i = 0; i < m || i < n; ++i) {
9189
if (i < m) {
92-
res.append(word1.charAt(i));
90+
ans.append(word1.charAt(i));
9391
}
9492
if (i < n) {
95-
res.append(word2.charAt(i));
93+
ans.append(word2.charAt(i));
9694
}
9795
}
98-
return res.toString();
96+
return ans.toString();
9997
}
10098
}
10199
```
@@ -107,20 +105,34 @@ class Solution {
107105
public:
108106
string mergeAlternately(string word1, string word2) {
109107
int m = word1.size(), n = word2.size();
110-
string res;
108+
string ans;
111109
for (int i = 0; i < m || i < n; ++i) {
112-
if (i < m) {
113-
res.push_back(word1[i]);
114-
}
115-
if (i < n) {
116-
res.push_back(word2[i]);
117-
}
110+
if (i < m) ans += word1[i];
111+
if (i < n) ans += word2[i];
118112
}
119-
return res;
113+
return ans;
120114
}
121115
};
122116
```
123117
118+
### **Go**
119+
120+
```go
121+
func mergeAlternately(word1 string, word2 string) string {
122+
m, n := len(word1), len(word2)
123+
ans := make([]byte, 0, m+n)
124+
for i := 0; i < m || i < n; i++ {
125+
if i < m {
126+
ans = append(ans, word1[i])
127+
}
128+
if i < n {
129+
ans = append(ans, word2[i])
130+
}
131+
}
132+
return string(ans)
133+
}
134+
```
135+
124136
### **Rust**
125137

126138
```rust

solution/1700-1799/1768.Merge Strings Alternately/README_EN.md

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,8 @@ merged: a p b q c d
6969
<p><strong>Constraints:</strong></p>
7070

7171
<ul>
72-
7372
<li><code>1 &lt;= word1.length, word2.length &lt;= 100</code></li>
74-
7573
<li><code>word1</code> and <code>word2</code> consist of lowercase English letters.</li>
76-
7774
</ul>
7875

7976
## Solutions
@@ -85,15 +82,7 @@ merged: a p b q c d
8582
```python
8683
class Solution:
8784
def mergeAlternately(self, word1: str, word2: str) -> str:
88-
i, m, n = 0, len(word1), len(word2)
89-
res = []
90-
while i < m or i < n:
91-
if i < m:
92-
res.append(word1[i])
93-
if i < n:
94-
res.append(word2[i])
95-
i += 1
96-
return ''.join(res)
85+
return ''.join(a + b for a, b in zip_longest(word1, word2, fillvalue=''))
9786
```
9887

9988
### **Java**
@@ -102,16 +91,16 @@ class Solution:
10291
class Solution {
10392
public String mergeAlternately(String word1, String word2) {
10493
int m = word1.length(), n = word2.length();
105-
StringBuilder res = new StringBuilder();
94+
StringBuilder ans = new StringBuilder();
10695
for (int i = 0; i < m || i < n; ++i) {
10796
if (i < m) {
108-
res.append(word1.charAt(i));
97+
ans.append(word1.charAt(i));
10998
}
11099
if (i < n) {
111-
res.append(word2.charAt(i));
100+
ans.append(word2.charAt(i));
112101
}
113102
}
114-
return res.toString();
103+
return ans.toString();
115104
}
116105
}
117106
```
@@ -123,20 +112,34 @@ class Solution {
123112
public:
124113
string mergeAlternately(string word1, string word2) {
125114
int m = word1.size(), n = word2.size();
126-
string res;
115+
string ans;
127116
for (int i = 0; i < m || i < n; ++i) {
128-
if (i < m) {
129-
res.push_back(word1[i]);
130-
}
131-
if (i < n) {
132-
res.push_back(word2[i]);
133-
}
117+
if (i < m) ans += word1[i];
118+
if (i < n) ans += word2[i];
134119
}
135-
return res;
120+
return ans;
136121
}
137122
};
138123
```
139124
125+
### **Go**
126+
127+
```go
128+
func mergeAlternately(word1 string, word2 string) string {
129+
m, n := len(word1), len(word2)
130+
ans := make([]byte, 0, m+n)
131+
for i := 0; i < m || i < n; i++ {
132+
if i < m {
133+
ans = append(ans, word1[i])
134+
}
135+
if i < n {
136+
ans = append(ans, word2[i])
137+
}
138+
}
139+
return string(ans)
140+
}
141+
```
142+
140143
### **Rust**
141144

142145
```rust

solution/1700-1799/1768.Merge Strings Alternately/Solution.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,11 @@ class Solution {
22
public:
33
string mergeAlternately(string word1, string word2) {
44
int m = word1.size(), n = word2.size();
5-
string res;
5+
string ans;
66
for (int i = 0; i < m || i < n; ++i) {
7-
if (i < m) {
8-
res.push_back(word1[i]);
9-
}
10-
if (i < n) {
11-
res.push_back(word2[i]);
12-
}
7+
if (i < m) ans += word1[i];
8+
if (i < n) ans += word2[i];
139
}
14-
return res;
10+
return ans;
1511
}
1612
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
func mergeAlternately(word1 string, word2 string) string {
2+
m, n := len(word1), len(word2)
3+
ans := make([]byte, 0, m+n)
4+
for i := 0; i < m || i < n; i++ {
5+
if i < m {
6+
ans = append(ans, word1[i])
7+
}
8+
if i < n {
9+
ans = append(ans, word2[i])
10+
}
11+
}
12+
return string(ans)
13+
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
class Solution {
22
public String mergeAlternately(String word1, String word2) {
33
int m = word1.length(), n = word2.length();
4-
StringBuilder res = new StringBuilder();
4+
StringBuilder ans = new StringBuilder();
55
for (int i = 0; i < m || i < n; ++i) {
66
if (i < m) {
7-
res.append(word1.charAt(i));
7+
ans.append(word1.charAt(i));
88
}
99
if (i < n) {
10-
res.append(word2.charAt(i));
10+
ans.append(word2.charAt(i));
1111
}
1212
}
13-
return res.toString();
13+
return ans.toString();
1414
}
1515
}
Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
11
class Solution:
22
def mergeAlternately(self, word1: str, word2: str) -> str:
3-
i, m, n = 0, len(word1), len(word2)
4-
res = []
5-
while i < m or i < n:
6-
if i < m:
7-
res.append(word1[i])
8-
if i < n:
9-
res.append(word2[i])
10-
i += 1
11-
return ''.join(res)
3+
return ''.join(a + b for a, b in zip_longest(word1, word2, fillvalue=''))

0 commit comments

Comments
 (0)