Skip to content

Commit d4ae825

Browse files
committed
Add solution #1768
1 parent 7f65a2b commit d4ae825

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,7 @@
337337
1672|[Richest Customer Wealth](./1672-richest-customer-wealth.js)|Easy|
338338
1716|[Calculate Money in Leetcode Bank](./1716-calculate-money-in-leetcode-bank.js)|Easy|
339339
1748|[Sum of Unique Elements](./1748-sum-of-unique-elements.js)|Easy|
340+
1768|[Merge Strings Alternately](./1768-merge-strings-alternately.js)|Easy|
340341
1780|[Check if Number is a Sum of Powers of Three](./1780-check-if-number-is-a-sum-of-powers-of-three.js)|Medium|
341342
1791|[Find Center of Star Graph](./1791-find-center-of-star-graph.js)|Easy|
342343
1812|[Determine Color of a Chessboard Square](./1812-determine-color-of-a-chessboard-square.js)|Easy|
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* 1768. Merge Strings Alternately
3+
* https://leetcode.com/problems/merge-strings-alternately/
4+
* Difficulty: Easy
5+
*
6+
* You are given two strings word1 and word2. Merge the strings by adding letters in alternating
7+
* order, starting with word1. If a string is longer than the other, append the additional
8+
* letters onto the end of the merged string.
9+
*
10+
* Return the merged string.
11+
*/
12+
13+
/**
14+
* @param {string} word1
15+
* @param {string} word2
16+
* @return {string}
17+
*/
18+
var mergeAlternately = function(word1, word2) {
19+
let result = '';
20+
for (let i = 0; i < Math.max(word1.length, word2.length); i++) {
21+
if (i < word1.length) result += word1[i];
22+
if (i < word2.length) result += word2[i];
23+
}
24+
return result;
25+
};

0 commit comments

Comments
 (0)