Skip to content

Commit d516e22

Browse files
committed
feat: add ts solution to lc problem: No.1768
No.1768.Merge Strings Alternately
1 parent 4bae8fb commit d516e22

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,20 @@ func mergeAlternately(word1 string, word2 string) string {
133133
}
134134
```
135135

136+
### **TypeScript**
137+
138+
```ts
139+
function mergeAlternately(word1: string, word2: string): string {
140+
const res = [];
141+
const n = Math.max(word1.length, word2.length);
142+
for (let i = 0; i < n; i++) {
143+
word1[i] && res.push(word1[i]);
144+
word2[i] && res.push(word2[i]);
145+
}
146+
return res.join('');
147+
}
148+
```
149+
136150
### **Rust**
137151

138152
```rust

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,20 @@ func mergeAlternately(word1 string, word2 string) string {
140140
}
141141
```
142142

143+
### **TypeScript**
144+
145+
```ts
146+
function mergeAlternately(word1: string, word2: string): string {
147+
const res = [];
148+
const n = Math.max(word1.length, word2.length);
149+
for (let i = 0; i < n; i++) {
150+
word1[i] && res.push(word1[i]);
151+
word2[i] && res.push(word2[i]);
152+
}
153+
return res.join('');
154+
}
155+
```
156+
143157
### **Rust**
144158

145159
```rust
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function mergeAlternately(word1: string, word2: string): string {
2+
const res = [];
3+
const n = Math.max(word1.length, word2.length);
4+
for (let i = 0; i < n; i++) {
5+
word1[i] && res.push(word1[i]);
6+
word2[i] && res.push(word2[i]);
7+
}
8+
return res.join('');
9+
}

0 commit comments

Comments
 (0)